Android:webView里面的对话框,URL没有加载

时间:2015-11-20 07:28:00

标签: java android webview android-webview

所以我想尝试使用webview在Dialog中加载URL。我也在Manifest文件中设置了Internet权限。但URL未加载。我不知道出了什么问题。我也尝试过this解决方案。

编辑:互联网连接不是问题因为我能够从模拟器浏览器浏览谷歌。 的 activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="25sp"
        android:text="Reddit OAuth2" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/auth"
        android:text="Sign in with Reddit" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Access"
        android:autoLink="all"
        android:textIsSelectable="true"
        android:layout_gravity="center"
        android:textSize="10sp"/>

</LinearLayout>

auth_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/webv"/>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

WebView web;
Button auth;
SharedPreferences pref;
TextView Access;
Dialog auth_dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Access =(TextView)findViewById(R.id.Access);
    auth = (Button)findViewById(R.id.auth);
    auth.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View arg0) {
           // TODO Auto-generated method stub
           auth_dialog = new Dialog(MainActivity.this);
           auth_dialog.setContentView(R.layout.auth_dialog);
           web = (WebView) auth_dialog.findViewById(R.id.webv);
           web.getSettings().setJavaScriptEnabled(true);

            web.loadUrl("http://www.google.com");

           web.setWebViewClient(new WebViewClient() {
               @Override
               public boolean shouldOverrideUrlLoading(WebView view, String url) {
                   view.loadUrl(url);
                   return true;
               }

           });
           auth_dialog.show();
           auth_dialog.setTitle("Authorize");
           auth_dialog.setCancelable(true);
             }
             });

  }
   }

Output i am getting:

这是我得到的输出。

1 个答案:

答案 0 :(得分:0)

你应该将shouldOverrideUrlLoading设置为返回false。现在您的代码使用内置浏览器而不是webview来加载URL

        web.setWebViewClient(new WebViewClient() {
           @Override
           public boolean shouldOverrideUrlLoading(WebView view, String url) {

               return false;
           }

       });