我正在尝试构建一个Web浏览器,后退时必须按下上一页才能出现在退出弹出对话框中。我在同一活动中使用了两个onBackpressed
方法。错误:方法onBackPressed()
已在类MainActivity
中定义。如何覆盖onBackPressed
方法或退出时销毁应用。
public class MainActivity extends AppCompatActivity {
ProgressBar superProgressBar;
ImageView superImageView;
WebView superWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
superProgressBar = findViewById( R.id.pg );
superWebView = findViewById( R.id.wv );
superImageView = findViewById( R.id.im );
superProgressBar.setMax( 100 );
superWebView.loadUrl( "https://www.google.com" );
superWebView.getSettings().setJavaScriptEnabled( true );
superWebView.setWebViewClient( new WebViewClient() );
superWebView.setWebChromeClient( new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged( view, newProgress );
superProgressBar.setProgress( newProgress );
}
@RequiresApi( api = Build.VERSION_CODES.KITKAT )
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle( view, title );
Objects.requireNonNull( getSupportActionBar() ).setTitle( title );
}
@Override
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon( view, icon );
superImageView.setImageBitmap( icon );
}
} );
}
@Override
public void onBackPressed() {
final AlertDialog.Builder builder = new
AlertDialog.Builder( MainActivity.this );
builder.setMessage( "" );
builder.setCancelable( true );
builder.setNegativeButton( "", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
} );
builder.setPositiveButton( "", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
finish();
}
} );
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate( R.menu.super_menu, menu );
return super.onCreateOptionsMenu( menu );
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if ( item.getItemId() == R.id.ref ) {
superWebView.reload();
}
return super.onOptionsItemSelected( item );
}
@Override
public void onBackPressed() {
if ( superWebView.canGoBack() ) {
superWebView.goBack();
} else {
finish();
}
}
}
答案 0 :(得分:2)
创建如下所示的方法
private void showExitPopup() {
final AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
builder.setMessage("");
builder.setCancelable(true);
builder.setNegativeButton("", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
builder.setPositiveButton("", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
finishAffinity();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
然后像这样覆盖onBackPressed方法
@Override
public void onBackPressed() {
if (superWebView.canGoBack()) {
superWebView.goBack();
}
else {
showExitPopup();
}
}