android webview 前进按钮

发布于 2024-10-14 04:32:08 字数 209 浏览 6 评论 0原文

我想知道如何在 android 的私人浏览器中禁用和启用前进菜单按钮。我正在编写一个应用程序,其中显示 3 个 webview,我想合并 android 自己的浏览器选项的基本功能。到目前为止一切正常wrt 功能。唯一的问题是“前进”菜单项始终处于启用状态。我也提到了 if canGoFwd then goFwd 的条件。问题是,我单独定义了菜单项并单独编写了转发功能,问题是动态更改“转发”按钮的状态

I want to know how to disable and enable forward menu button as in android's private browser.I am writing an app where I am displaying 3 webviews and I want to incorporate the basic functionality of android's own browser options.Everything is working fine as of now w.r.t functionality. Only thing is 'forward'menu item is always enabled.I have mentioned conditoin too for if canGoFwd then goFwd. Thing is, I have defined menu items separately and wrote forward function separately, problem is dynamically changing states for 'forward' button

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很快妥协 2024-10-21 04:32:08
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>
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 file

<?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文