简体   繁体   中英

when i open a link in android webview in app browser how can i put close button on top left

Please Refer this example http://www.mkyong.com/android/android-webview-example/

In the above link example when we clink on button it redirects to google home page. Google page opening in webview. On the top i want to put close button. So when i click close button it has to go to previous back page. Please see below image for example.

屏幕截图

public class MainActivity extends Activity {
    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar bar = getActionBar();
        bar.hide();
        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.google.com");
        webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView viewx, String urlx) {
                viewx.loadUrl(urlx);
                return false;
            }
        });

    }
}

Remove these lines from your code:

 ActionBar bar = getActionBar();
 bar.hide();

Now replace your home button on action bar with an 'X' icon and override its action like this :

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case android.R.id.home:
          finish();
    }
    return (super.onOptionsItemSelected(menuItem));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM