gpt4 book ai didi

Android webview 后退按钮,无法正常工作

转载 作者:太空狗 更新时间:2023-10-29 16:23:35 25 4
gpt4 key购买 nike

我已经创建了我的第一个 Android 应用程序,它基本上是一个 webview 来在线调用一些页面,但是当我按下后退按钮时它只是关闭应用程序,我如何配置后退按钮以返回页面?

这是我当前的代码:

 package com.mysite.myapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class mysite extends Activity
{

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(mWebView.canGoBack() == true){
mWebView.goBack();
}else{
finish();
}
return true;
}

}
return super.onKeyDown(keyCode, event);
}

WebView webView;

final Activity activity = this;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
final WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
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;
}
});
webView.loadUrl("http://www.mysite.com/page1.php");

Button webButton = (Button) findViewById(R.id.button1);
webButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
webView.loadUrl("http://www.mysite.com/page2.php");
}});

Button webButton1 = (Button) findViewById(R.id.button2);
webButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
webView.loadUrl("http://www.mysite.com/page3.php");
}});

Button webButton2 = (Button) findViewById(R.id.button3);
webButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
webView.loadUrl("http://www.mysite.com/page4.php");
}});

Button webButton3 = (Button) findViewById(R.id.button4);
webButton3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
webView.loadUrl("http://www.mysite.com/page4.php");
}});

Button webButton4 = (Button) findViewById(R.id.button5);
webButton4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), morescreen.class);
startActivityForResult(myIntent, 0);
}

});

Button webButton5 = (Button) findViewById(R.id.button6);
webButton5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
webView.loadUrl("http://www.mysite.com/page5.php");
}});



}
}

非常感谢您的帮助,我还在学习:)

提前致谢

周杰伦

最佳答案

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}

关于Android webview 后退按钮,无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8142051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com