【问题标题】:android webview forward buttonandroid webview 前进按钮
【发布时间】:2011-01-24 17:52:31
【问题描述】:

我想知道如何在 android 的私人浏览器中禁用和启用前进菜单按钮。我正在编写一个显示 3 个 web 视图的应用程序,我想结合 android 自己的浏览器选项的基本功能。一切正常截至目前 w.r.t 功能。唯一的问题是始终启用“前进”菜单项。我也提到了条件,如果 canGoFwd 然后 goFwd。问题是,我已经单独定义了菜单项并单独编写了转发功能,问题是动态改变“转发”按钮的状态

【问题讨论】:

    标签: android webview forward


    【解决方案1】:
    private WebView webView;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
    
        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        final Button previousButton =(Button) findViewById(R.id.previousButton);
    
        previousButton.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                   if(webView.canGoBack()){
                       webView.goBack();
                   }
            }
        });
        final Button forwardButton =(Button) findViewById(R.id.forwardButton);
    
        forwardButton.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                   if(webView.canGoForward()){
                       webView.goForward();
                   }
            }
        });
        webView.setWebViewClient( new WebViewClient() {
    
    
    
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
            }
    
            @Override
            public void onPageFinished( WebView view, String url ) {
    
                super.onPageFinished(webView, url );
    
                previousButton.setEnabled(view.canGoBack());
                forwardButton.setEnabled(view.canGoForward());
    
            }
    
            @Override
            public void onReceivedError( WebView view, int errorCode, String description, String failingUrl ) {
    
                super.onReceivedError( webView, errorCode, description, failingUrl );
                Toast.makeText( WebViewActivity.this, description, Toast.LENGTH_LONG );
            }
        } );
        webView.loadUrl("");
    

    webview.xml 文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <WebView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/webView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    
        <Button
            android:id="@+id/previousButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:enabled="false" 
            android:text="previous"/>
    
        <Button
            android:id="@+id/forwardButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/previousButton"
            android:text="forward"
            android:enabled="false" />
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 2011-01-20
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多