Need help changing from stock android browser to webview

南楼画角 提交于 2019-12-31 03:00:36

问题


Ok, so I've got my app done, and I'm working on minor tweaks for it, and one thing is I would prefer my web links to launch in webview instead of the stock browser... I've tried so many things and I keep getting errors and eclipse keeps launching the debug perspective and I'm stuck..

// XXXX.edu Button
        Button XXXX_Button = (Button)findViewById( R.id.XXXX_Button );
        XXXXXX_Button.setOnClickListener( new View.OnClickListener()
        {
        public void onClick(View v)
        {
            Uri uri = Uri.parse( "http://www.XXXXXX.edu/" );
            startActivity( new Intent( Intent.ACTION_VIEW, uri ) );
        }
        });

回答1:


You need to extend WebViewClient, and launch the url within that.

public class WebActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        webview = (WebView) findViewById(R.id.wv);
        webview.setWebViewClient(new WebC());
        webview.loadUrl(baseUrl);
    }

    public class WebC extends WebViewClient
    {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            super.onReceivedError(view, errorCode, description, failingUrl);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
    ... etc.

And in your layout xml,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

    <WebView
        android:id="@+id/wv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>


来源:https://stackoverflow.com/questions/5483030/need-help-changing-from-stock-android-browser-to-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!