如何禁用在Android中按下webview的后退按钮?

时间:2015-09-18 10:20:42

标签: android webview

如何禁用在android中按下webview的后退按钮?

string u = "select uniqcode from login where users='" + textBoxX1.Text + "' ";

6 个答案:

答案 0 :(得分:3)

如果您希望在WebView可见并启用后退按钮操作时禁用后退按钮操作,如果WebView处于不可见,请在您的活动中尝试以下代码

@Override
public void onBackPressed() {
   if(webview.getVisibility()==View.VISIBLE){
      // dont pass back button action
      if(webview.canGoBack()){
         webview.goBack();
      }
      return;
   }else{
      // pass back button action
      super.onBackPressed();
   }
}

答案 1 :(得分:1)

有很多方法可以实现,

解决方案1,重写dispatchKeyEvent()

  

dispatchKeyEvent()(API Level 1,Android 1.0)

请参阅我的回答use dispatchKeyEvent to disable back button

if( $a =~ m!Y!i ){
    $a = "display something";  //some sentence to display
}

解决方案2,覆盖onBackPressed()

  

onBackPressed()(API Level 5,Android 2.0)

请参阅Use onBackPressed() to disable back button

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    // TODO Auto-generated method stub
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        return true;
    }
    return super.dispatchKeyEvent(event);
}

解决方案3,覆盖onKeyDown()

  

onKeyDown()(API Level 1,Android 1.0)

请参阅Use onKeyDown() to disable back button

@Override
public void onBackPressed() {
}

答案 2 :(得分:0)

您已在“活动”中添加以下代码,以便重新启用禁用活动

@Override
public void onBackPressed() {

}

答案 3 :(得分:0)

只需覆盖onBackPressed()方法。

@Override
public void onBackPressed() { }

答案 4 :(得分:0)

请试试这个

   @Override
public void onBackPressed() {
      if(webview.canGoBack()){
         webview.goBack();
      }
    else{
      super.onBackPressed();
   }
}

答案 5 :(得分:0)

Add this below code in java file : 

WebView mwebView;
Button backButton1;

  backButton1 = findViewById(R.id.backButton1);
 mwebView = findViewById(R.id.mwebView);

backButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mWebView.destroy();
            }
        });