在这种情况下,我在我的html页面中有一些divs元素显示位于资产文件夹中,我使用WebView加载该页面。我想要的是当我按下backButton然后它隐藏所有divs元素。但问题是当backButton按下它关闭我的应用程序。有没有办法实现这个目标?
答案 0 :(得分:0)
我观察到的是,您在 webview 中加载了网址活动,并且您现在想要在中加载页面上的某些操作时覆盖活动的后退事件>网页视图强>
试试这个技巧,拿一个布尔值,在webview中加载页面后将其设为true,并在backpress事件中检查相同的布尔值。
public void onBackPressed() {
if(boolean){
//do action
}else{
super.onBackPressed();
}
}
答案 1 :(得分:0)
公共类AndroidBackButtonActivity扩展了Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(getApplicationContext(),"http://programmerguru.com/android-tutorial", Toast.LENGTH_LONG).show();
}
@Override
public void onBackPressed() {
//Display alert message when back button has been pressed
backButtonHandler();
return;
}
public void backButtonHandler() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
AndroidBackButtonActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Leave application?");
// Setting Dialog Message
alertDialog.setMessage("Are you sure you want to leave the application?");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.dialog_icon);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
}
答案 2 :(得分:0)
问题已解决... onBackPressed方法我使用webview加载javascript
@Override
public void onBackPressed(){
WebView webView = (WebView) findViewById(R.id.webView1);
webView.loadUrl("javascript:$('#settingEdit').hide()");
}
答案 3 :(得分:-1)
public void onBackPressed() {
// Write your code here
super.onBackPressed();
}