【问题标题】:open a url on click of ok button in android在android中点击ok按钮打开一个url
【发布时间】:2011-06-23 05:56:11
【问题描述】:

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

【问题讨论】:

  • public void openWebURL( String inURL ) { Intent browse = new Intent( Intent.ACTION_VIEW , Uri.parse( inURL ) );开始活动(浏览); }
  • 这会很好用,伙计.. 所以 1 up...
  • @tushar:你试过了吗?我认为它应该可以正常工作。运行此代码时是否遇到任何错误?

标签: android button click


【解决方案1】:

Button点击事件上写这个:

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

打开您的网址。

【讨论】:

  • startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"))
  • @Chris-Jr 你错过了最后一个括号,即)
  • 这与打开 webview 项目有何不同?
【解决方案2】:
    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);
        }
    });

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      创建一个意图并为其设置一个操作,同时将 url 传递给意图

      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】:
        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 是否有效。

        【讨论】:

          【解决方案6】:

          无需任何 Java 或 Kotlin 代码即可使其成为可点击的链接,现在您只需按照下面给出的代码进行操作即可。您还可以使用 textColorLink 链接文本颜色更改。

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

          【讨论】:

            【解决方案7】:

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

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

            【讨论】:

              【解决方案8】:
               private fun goToUrl(url: String) {
                      val uriUrl = Uri.parse(url)
                      val launchBrowser = Intent(Intent.ACTION_VIEW, uriUrl)
                      startActivity(launchBrowser)
                  }
              

              【讨论】:

              • 你能解释一下吗?
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2013-06-10
              • 1970-01-01
              • 2014-11-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-10-02
              相关资源
              最近更新 更多