我尝试在其他一些问题之后打开一个网址,但是onClick事件只是重新启动应用而不是打开浏览器。谢谢你的帮助
更新:这适用于不支持片段的活动。
这是我的.xml按钮
<ImageButton
android:layout_width="55dp"
android:layout_height="55dp"
android:id="@+id/imageButton2"
android:background="@drawable/icon"
android:layout_margin="5dp"
android:layout_weight="1"
android:onClick="EnterButton"/>
这是我的.java和实现的方法。
public void EnterButton(View view) {
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
答案 0 :(得分:0)
而不是这个
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
只需使用此
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
完整代码:
public void EnterButton(View view) {
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
}
答案 1 :(得分:0)
使用此代码作品Man ...
public void EnterButton(View view) {
String url = "http://www.google.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
答案 2 :(得分:0)
它旨在处理不在片段上的活动,参考:https://stackoverflow.com/a/21192511/3111083
但你可以在片段上做到这一点。
ImageButton button = (ImageButton) view.findViewById(R.id.imageButton2);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// do something
}
});
答案 3 :(得分:0)
经过一些尝试,这对我的片段起作用了。接口中的其他方法不会被修改。
public class HomeFragment extends Fragment implements View.OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
ImageButton b = (ImageButton) v.findViewById(R.id.imageButton2);
b.setOnClickListener(this);
return v;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButton2:
String url = "http://www.google.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
break;
}
}