将网页链接到Android中的按钮
我正在做一个android项目。。我想能够点击一个按钮,它会直接把我带到一个网页 很抱歉提供了这么多信息,但我真的不知道怎么做。 对于按钮,这是我的xml代码。。 我不确定是否必须在xml或MainActivity文件中编写代码将网页链接到Android中的按钮,android,eclipse,Android,Eclipse,我正在做一个android项目。。我想能够点击一个按钮,它会直接把我带到一个网页 很抱歉提供了这么多信息,但我真的不知道怎么做。 对于按钮,这是我的xml代码。。 我不确定是否必须在xml或MainActivity文件中编写代码 <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:la
<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"/>
将以下内容添加到按钮xml中
android:onClick="openSite"
在Java中
public void openSite(View v){
Uri uri = Uri.parse("http://myurl");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
试试这个
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);
}
});