在Android中禁用後退按鈕

註銷應用程序時如何在android中禁用後退按鈕? android


#1樓

我正在使用............. app

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode==KeyEvent.KEYCODE_BACK)
        Toast.makeText(getApplicationContext(), "back press",      
     Toast.LENGTH_LONG).show();

    return false;
       // Disable back button..............
}

#2樓

若是您正在使用FragmentActivity 。 而後像這樣 ide

首先在您的Fragment調用This。 spa

public void callParentMethod(){
    getActivity().onBackPressed();
}

而後在父FragmentActivity類的旁邊調用onBackPressed方法。 code

@Override
public void onBackPressed() {
  //super.onBackPressed();
  //create a dialog to ask yes no question whether or not the user wants to exit
  ...
}

#3樓

只需重寫onBackPressed()方法。 get

@Override
public void onBackPressed() { }

#4樓

您能夠經過如下簡單方法進行操做:不要調用super.onBackPressed() it

注意:-除非有充分的理由,不然請不要這樣作。

@Override
public void onBackPressed() {
// super.onBackPressed();
// Not calling **super**, disables back button in current screen.
}

#5樓

您只須要覆蓋後退按鈕的方法便可。 您能夠根據須要將方法保留爲空,以便在按「後退」按鈕時不進行任何操做。 請看下面的代碼: io

@Override
public void onBackPressed() 
{
   // Your Code Here. Leave empty if you want nothing to happen on back press.
}
相關文章
相關標籤/搜索