back and forward button for webview in android. how?

China☆狼群 提交于 2020-08-24 07:27:15

问题


I'm working on a webview. If I click anything in my webview, it redirects to another link by using webviewclient class.

Now I put a footer contains back and forward button, to do the functionality which in ordinary browser.

I can work about to back and forward of webview. It works fine.

Now I want to set a buttons in the footer are initially should be not focused and it should work clickable and not clickable dynamically.


回答1:


I solved this problem using canGoBack() and canGoforward(), we can disable those buttons using obj_name.setEnabled(false);.

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




回答2:


If your layout like

<?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" />

Now we can implement that like this...

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


来源:https://stackoverflow.com/questions/2317280/back-and-forward-button-for-webview-in-android-how

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