使用手机后退按钮退出Webview

编程入门 行业动态 更新时间:2024-10-17 19:33:58
本文介绍了使用手机后退按钮退出Webview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

电话后退按钮可以在Web视图中使用,但是当我想从Web视图退出到应用程序时,它什么也没做. 我放了一个可以返回到应用程序的按钮,但我没有那样保留.

The phone back button works in the webview,but when I want to exit from the Webview to the application this didn't do anything. I put a button that go back to the application but I did't that remain that way.

JAVA:

package com.wiralss.adb; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; public class about extends Activity implements OnClickListener{ WebView webView; final Activity activity = this; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.getWindow().requestFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.instagrem); webView = (WebView) findViewById(R.id.webView1); Button B123=(Button)findViewById(R.id.button1235); B123.setOnClickListener(this); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl("www.google"); webView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { activity.setTitle("Loading..."); activity.setProgress(progress * 100); if(progress == 100) activity.setTitle(R.string.app_name); } }); webView.setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { // Handle the error } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.button1235: Intent B = new Intent(this, MainActivity.class); startActivity(B); break; // TODO Auto-generated method stub } // TODO Auto-generated method stub } public void onBackPressed (){ if (webView.isFocused() && webView.canGoBack()) { webView.goBack(); } else { super.onBackPressed(); finish(); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { webView.goBack(); return true; } return false; } }

XML:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="schemas.android/apk/res/android" xmlns:tools="schemas.android/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:orientation="vertical" > <Button android:id="@+id/button1235" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="580px" android:layout_weight="0.05" android:text="Button" /> </LinearLayout> </RelativeLayout>

另一个问题. 要删除按钮,我需要删除吗?:

Another question. To remove the button I need to delete?:

Button B123=(Button)findViewById(R.id.button1); B123.setOnClickListener(this);

然后?

@Override public void onClick(View v) { switch(v.getId()){ case R.id.button1: Intent B = new Intent(this, MainActivity.class); startActivity(B); break; // TODO Auto-generated method stub } // TODO Auto-generated method stub }

非常感谢你

推荐答案

您无需调用super.onBackPressed(),因为您可以自己控制活动的完成.

You don't need the call to super.onBackPressed() as you are controlling the finishing of your activity yourself.

同样,您实际上不需要webView.isFocused()方法调用.

Equally, you don't really need the webView.isFocused() method call.

您可以重写您的onBackPressed方法,使其看起来像这样:

You can rewrite your onBackPressed method to read like this:

public void onBackPressed() { if (webView.canGoBack()) { webView.goBack(); } else { finish(); } }

对于包含button1235按钮的动机,我不太确定,是您要删除的内容吗?

I'm not too sure on your motives for including your button1235 Button, is that something you are wanting to remove?

更多推荐

使用手机后退按钮退出Webview

本文发布于:2023-11-27 07:42:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637155.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   手机   Webview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!