0

我想从 EditField 打开一个字符串,然后按下按钮,页面应该在我的 WebView 中打开。而不是我的网址默认打开网络浏览器

XML:

在此处输入图像描述

代码:

public class WebAdress extends Activity {

WebView webView;
ImageButton buttonGo;
EditText textWWW;
String URL = new String();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.www);

    buttonGo = (ImageButton) findViewById(R.id.imageButton1);
    textWWW  = (EditText) findViewById(R.id.editText1);
    webView = (WebView) findViewById(R.id.webView1);

    buttonGo.setOnClickListener(new OnClickListener() {

        @SuppressLint("SetJavaScriptEnabled")
        public void onClick(View v) {
            URL = "http://" + textWWW.getText().toString();
            WebSettings settings = webView.getSettings();
            settings.setJavaScriptEnabled(true);
            webView.loadUrl(URL);
            URL = null;
        }
    });

}

}

4

2 回答 2

3

Try with this.

// workaround so that the default browser doesn't take over
   webView.setWebViewClient(new MyWebViewClient());

 private class MyWebViewClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        } 

See this Demo .

于 2012-09-05T10:49:46.110 回答
3

first check your menifest file to check internet permission and then try this code to load url in to webview. his code works for me for the same view.

webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl(YOUR ANY URL);
webview.setWebViewClient(new webviewClient());

class webviewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

Hope it will help you.

于 2012-09-05T10:50:07.490 回答