【问题标题】:back and forward button for webview in android. how?android中webview的后退和前进按钮。如何?
【发布时间】:2011-01-20 00:15:36
【问题描述】:

我正在处理webview。如果我单击 webview 中的任何内容,它会使用 webviewclient 类重定向到另一个链接。

现在我放了一个包含backforward按钮的页脚,来做普通浏览器中的功能。

我可以在webviewbackforward 工作。它工作正常。

现在我想在页脚中设置一个按钮,最初不应该聚焦,它应该可以点击而不是动态点击。

【问题讨论】:

  • 您不断删除并重新添加相同的问题。也许你应该重写这个问题。人们不回答您的问题可能是有原因的。一方面,我完全不知道最后一句话是什么意思。
  • 当 webview 最初加载时,没有 forward(canGoForward()) 或 backward(canGoBack()) 选项,不是吗?然后我想设置那些按钮不应该聚焦或点击。就是这样,伙计。

标签: android android-emulator webview


【解决方案1】:

我使用canGoBack()canGoforward() 解决了这个问题,我们可以使用obj_name.setEnabled(false); 禁用这些按钮。

Here's a good sample from Google's, worth looking at.

【讨论】:

    【解决方案2】:

    如果你的布局喜欢

    <?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/backButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:enabled="false" 
        android:text="Back"/>
    
    <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" />
    

    现在我们可以这样实现...

    private WebView webView;
    
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
    
    //Web View Initialization
    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    
    //Button Initialization
    final Button backButton =(Button) findViewById(R.id.backButton);
    final Button forwardButton =(Button) findViewById(R.id.forwardButton);
    
    //Back Button Action
    backButton.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
    
               // Going back if canGoBack true
               if(webView.canGoBack()){
                   webView.goBack();
               }
        }
    });
    //Forward Button Action
    forwardButton.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            // Go Forward if canGoForward is frue 
    
               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 );
    
            //Make Enable or Disable buttons
            backButton.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("www.google.com");//Your url goes hare
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多