2014-04-15 56 views
0

我正在制作一个android项目..我希望能够点击一个按钮,它会直接将我带到网页。将网页链接到Android中的按钮

对不起,我很少有信息,但我真的不知道该怎么做。 对于按钮,这是XML代码我.. 我不知道如果我在XML或者在MainActivity文件代码..

<Button 
    android:id="@+id/button2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/button1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="30dp" 
    android:text="@string/socs_button" 
    android:textColor="#0000CD" 
    android:textSize="20sp"/> 

回答

1

以下内容添加到您的按钮XML

android:onClick="openSite" 

在控制actvity的Java

public void openSite(View v){ 
    Uri uri = Uri.parse("http://myurl"); 
    Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
    startActivity(intent); 
} 
+0

工作很好,谢谢! – DigitalD

0

试试这个..

Button button2 = (Button) dialog.findViewById(R.id.button2); 
    button2.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setData(Uri.parse("your site name")); 
      startActivity(intent); 
     } 
    });