在android中点击ok按钮打开一个url

时间:2011-02-08 06:30:53

标签: android button click

我必须在视图中点击OK按钮打开一个URL。有人可以告诉你怎么做吗?

7 个答案:

答案 0 :(得分:215)

Button点击事件中写下:

Uri uri = Uri.parse("http://www.google.com"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

打开您的网址。

答案 1 :(得分:4)

    Button imageLogo = (Button)findViewById(R.id.iv_logo);
    imageLogo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String url = "http://www.gobloggerslive.com";

            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
        }
    });

答案 2 :(得分:1)

String url = "https://www.murait.com/";
if (url.startsWith("https://") || url.startsWith("http://")) {
    Uri uri = Uri.parse(url);
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
}else{
    Toast.makeText(mContext, "Invalid Url", Toast.LENGTH_SHORT).show();
}

您必须检查URL是否有效。如果URL无效,则应用程序可能会崩溃,因此您必须通过此方法检查URL是否有效。

答案 3 :(得分:0)

您可以使用以下方法,该方法会将您的目标网址作为唯一输入(不要忘记http://)

void GoToURL(String url){
    Uri uri = Uri.parse(url);
    Intent intent= new Intent(Intent.ACTION_VIEW,uri);
    startActivity(intent);
}

答案 4 :(得分:0)

创建一个intent并为其设置一个动作,同时将url传递给intent

yourbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String theurl = "http://google.com";
                Uri urlstr = Uri.parse(theurl);
                Intent urlintent = new Intent();
                urlintent.setData(urlstr);
                urlintent.setAction(Intent.ACTION_VIEW);
                startActivity(urlintent);

答案 5 :(得分:0)

不需要任何Java或Kotlin代码使其成为可点击的链接,现在您只需要遵循以下给定的代码即可。您还可以使用textColorLink链接文本颜色更改。

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:textColorLink="@color/white"/>

答案 6 :(得分:0)

将此代码添加到您的“确定”按钮单击侦听器中。

startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")))
相关问题