为什么浏览器会打开另一个浏览器?

时间:2015-02-14 12:53:23

标签: android browser

我正在创建一个Android应用程序,作为一个简单的浏览器。用户输入网址,然后单击按钮。在此之后,应用程序应该在WebView中的网站。但是,单击该按钮后,会弹出一个通知,要求我选择一个浏览器来完成我的操作。 [弹出窗口中的选项是我的默认浏览器,mozilla等。]

但是,我不想使用它们中的任何一个。我该怎么办?

这是我的.java文件:

package com.example.all_in_one;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;

public class SimpleBrowser extends Activity implements OnClickListener{

    WebView myBrowser;
    EditText URLaddress;
    Button Go;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_browser);

        myBrowser = (WebView) findViewById(R.id.WV);
        URLaddress = (EditText) findViewById(R.id.webaddress);
        Go = (Button) findViewById(R.id.go);
        Go.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.go:
            String address = URLaddress.getText().toString();
            myBrowser.loadUrl("http://" + address);
            break;
        }
    }
}

这是我的.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/LL1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:weightSum="5" >
        <EditText 
            android:id="@+id/webaddress"
            android:inputType="textNoSuggestions"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            />
        <Button 
            android:id="@+id/go"
            android:text="Go"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="4"
            />

    </LinearLayout>

    <WebView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/WV"
        android:layout_below="@+id/LL1" />
</RelativeLayout>

我已在我的清单文件中添加了Internet权限。

请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:1)

您必须将WebViewClient类对象传递给browser.setWebViewClient(new BrowserClient())才能打开网页视图中的链接。

private class BrowserClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.d(tag, "shouldOverrideUrlLoading");
        view.loadUrl(url);
        return true;
    }
}

并在你的onCreate()调用中

myBrowser.setWebViewClient(new BrowserClient());
相关问题