Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在默认浏览器或webview中打开按钮单击url_Android_Android Layout_Android Intent - Fatal编程技术网

Android 如何在默认浏览器或webview中打开按钮单击url

Android 如何在默认浏览器或webview中打开按钮单击url,android,android-layout,android-intent,Android,Android Layout,Android Intent,单击按钮后,如何在webview或默认浏览器中打开url?目前,当我点击btn1按钮时,它会提示我从手机中选择浏览器。我想在默认浏览器中或在webview中打开此url 以下是我的java代码: public class myactivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

单击按钮后,如何在
webview
或默认浏览器中打开url?目前,当我点击
btn1
按钮时,它会提示我从手机中选择浏览器。我想在默认浏览器中或在
webview
中打开此url

以下是我的java代码:

 public class myactivity extends Activity {

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button bt1 = (Button) findViewById(R.id.btn_click_login);
    btn_login.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
             Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
             myWebLink.setData(Uri.parse("http://google.com"));
             startActivity(myWebLink);
          }
         }
     );
}

在android中,有两种类型的意图:

显式意图:在指定目标组件的地方,显式地。 隐式意图:未指定目标组件,而是提供了一些其他字段,如数据、操作、类别。。并且根据这些字段(属性),android系统过滤活动或组件以处理意图


您使用的是隐式意图,它将列出可以在Action_视图上工作的所有活动,以及指定的URI。为了避免这种情况,您只有一个选择,您可以限制Android系统通过其他参数进行进一步过滤,或者将其更改为显式意图。若要将其更改为显式意图,您需要目标组件名称。

可以尝试使用您的意图

intent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
在代码中

 public void onClick(View v) {
             Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
             myWebLink.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
             myWebLink.setData(Uri.parse("http://google.com"));
             startActivity(myWebLink);
       }
给你

确保在清单中包含

Intent internetIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com"));
internetIntent.setComponent(new ComponentName("com.android.browser","com.android.browser.BrowserActivity"));
internetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(internetIntent);

在布局xml中放置WebView

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"

  android:gravity="center"  
  >

  <WebView 
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/web"
/>

</RelativeLayout>
对其调用loadUrl()

这对我有用

        public void onClick(View v) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
            startActivity(browserIntent);
        }

你问了10个问题,但没有一个被美国政府接受。事件你有正确的答案吗?正如@imrankhan也提到的,如果你接受正确的答案,你会有更好的运气得到帮助。兄弟们,我没有得到正确的答案,如果我得到一个,我会接受的。。对不起,我运气不好@sam这个问题甚至没有得到回答,我得到了一个答案,但那是针对android浏览器的,不是针对webview的。@LaChi你的老问题没有得到回答并不是再次发布的理由。如果你这样回复,你就是在“乱丢”网站。另外,你想做的是非常基本的,如果你在这个网站和开发者文档中搜索,你可以找到无数的例子。它只需在webview中加载这个url,但我想加载那个url,当用户点击按钮时就会打开。android.content.ActivityNotFoundException:无法找到显式活动类
mWeb.loadUrl("http://google.com");
        public void onClick(View v) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
            startActivity(browserIntent);
        }