在這種情況下,我有一些div元素顯示在我的HTML頁面位於資產文件夾中,我使用WebView加載該頁面。我想要的是當我按backButton然後它隱藏所有的div元素。但問題是當backButton按下它關閉我的應用程序。有沒有辦法做到這一點?如何覆蓋android中的backButton默認方法?
0
A
回答
-1
public void onBackPressed() {
// Write your code here
super.onBackPressed();
}
0
我觀察,你加載網址在的WebView這是一個活動和重寫活動的背部現在事件要加載網頁上的某些行動的WebView。
試試這個技巧,取一個布爾值,在webview中加載一次頁面並在後退事件中檢查相同的布爾值時使其成爲true。
public void onBackPressed() {
if(boolean){
//do action
}else{
super.onBackPressed();
}
}
0
公共類AndroidBackButtonActivity延伸活動{
@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();
}
}
+0
你的代碼工作完美,但它不是我正在尋找...謝謝 – 2015-03-13 07:46:50
0
問題就迎刃而解了...... onBackPressed方法我使用的WebView
@Override
public void onBackPressed(){
WebView webView = (WebView) findViewById(R.id.webView1);
webView.loadUrl("javascript:$('#settingEdit').hide()");
}
相關問題
- 1. Android ActionBar Backbutton默認填充
- 2. 如何覆蓋django-rest-framework中的默認創建方法
- 3. 如何覆蓋Resources.Designer.cs中的默認System.Resources.ResourceManager?
- 4. 覆蓋默認值
- 5. 覆蓋默認的Android主題
- 6. 如何覆蓋表單的默認值?
- 7. 如何覆蓋默認的nlog配置?
- 8. 如何覆蓋默認的window.scrollTo函數?
- 9. Android方法覆蓋
- 10. 如何覆蓋Qt中的默認快捷方式
- 11. 無法覆蓋默認的WCF配置
- 12. 無法覆蓋默認取消目的
- 13. Android覆蓋默認對話框主題
- 14. 覆蓋android默認相機/活動
- 15. 覆蓋TextBox.TextProperty的默認值
- 16. 用默認的覆蓋app.config
- 17. 覆蓋默認的jQuery css?
- 18. 如何在想法中配置默認的覆蓋跑步者?
- 19. 如何覆蓋默認呼叫屏幕?
- 20. 如何覆蓋默認鍵盤佈局?
- 21. 如何覆蓋默認佈局
- 22. 如何覆蓋「默認」導航欄?
- 23. 如何用默認配置覆蓋CSS?
- 24. 軌道 - 如何覆蓋默認視圖
- 25. 如何覆蓋默認元素.styles
- 26. 如何覆蓋默認功能
- 27. Android:如何覆蓋撥號器的默認行爲
- 28. 如何覆蓋在JavaScript中的默認確認/ jquery
- 29. Jquery覆蓋默認定位
- 30. 覆蓋JComboBox默認顏色
請你[編輯加載JavaScript ]解釋爲什麼這段代碼回答這個問題?僅限代碼答案[不鼓勵](http://meta.stackexchange.com/questions/148272),因爲他們沒有教導解決方案。 – DavidPostill 2015-03-13 06:40:03
感謝您的回答,但它不完全是我想要的;)它仍然關閉我的應用程序。 – 2015-03-13 07:45:03