问题
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("https://www.google.com");
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="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/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);
And?
@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
}
Tank you very much.
回答1:
You don't need the call to super.onBackPressed()
as you are controlling the finishing of your activity yourself.
Equally, you don't really need the webView.isFocused()
method call.
You can rewrite your onBackPressed
method to read like this:
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
}
I'm not too sure on your motives for including your button1235
Button, is that something you are wanting to remove?
回答2:
Remove onKeyDown
method implementation as the way you implement it will case onBackPressed
to never be called. onBackPressed
should be only:
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
To remove the button it's enough to make it gone, but first of all you need to declare it as class member:
private Button B123;
and initialize it as you do in onCreate
:
webView = (WebView) findViewById(R.id.webView1);
B123 = (Button) findViewById(R.id.button1235);
B123.setOnClickListener(this);
To make it gone:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1235:
B123.setVisibility(View.GONE);
Intent B = new Intent(this, SecondActivity.class);
startActivity(B);
break;
// TODO Auto-generated method stub
}
// TODO Auto-generated method stub
}
I would change the layout as well as you're getting some lint errors since you're using px
instead of dp
. The whole layout can be improved.
来源:https://stackoverflow.com/questions/17594339/using-phones-back-button-to-exit-from-webview