2011-09-07 68 views
0

我想在用戶按下手機時顯示alertbox。 所以乾脆我用::AlertBox在後面按下android

public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if ((keyCode == KeyEvent.KEYCODE_BACK)) { 
      try { 
        AlertDialog.Builder alertbox = new AlertDialog.Builder(this); 
        alertbox.setMessage("This is the alertbox!"); 
        alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 


         public void onClick(DialogInterface arg0, int arg1) { 
          Toast.makeText(getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_SHORT).show(); 
         } 
        }); 

        // set a negative/no button and create a listener 
        alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() { 


         public void onClick(DialogInterface arg0, int arg1) { 
          Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show(); 
         } 
        }); 

        alertbox.show(); 
      stopService(new Intent(this, BackServices.class)); 
       finish(); 
      } catch (Exception e) { 
       // TODO: handle exception 
      } 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

but problem is:當我按下回alertbox節目,但它從應用程序退出。我想停止從應用程序退出,直到用戶按是button.if用戶按下沒有再回到應用

回答

2

那麼有兩件事情使人們有可能

1)

stopService(new Intent(this, BackServices.class)); 
       finish(); 

從這裏刪除finish();,因爲這是您的活動接近的原因

2)而不是

return super.onKeyDown(keyCode, event); 

可以返回false這樣,

return false; 
+0

這是很好的解決方案 –

+0

我已經使用第二個數字選項 –

+1

現在你能告訴我如何讓警報框變得豐富多彩或有吸引力 –

0

我的做法會是這樣,

public void onBackPressed() { 
    //finish(); 
    AlertDialog.Builder alertbox = new AlertDialog.Builder(this); 
    alertbox.setTitle("Warning"); 
    alertbox.setMessage("Exit Application?"); 
    alertbox.setPositiveButton("Yes", new 
    DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface arg0, int arg1) { 
    finish(); 
    } 
    }); 
    alertbox.setNegativeButton("No", new 
    DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface arg0, int arg1) { 

    } 
    }); 
    alertbox.show(); 
} 

或考慮自己的代碼,我可以做什麼喜歡這個。

你必須提供完成()這樣的肯定按鈕點擊事件中,

public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if ((keyCode == KeyEvent.KEYCODE_BACK)) { 
     try { 
       AlertDialog.Builder alertbox = new AlertDialog.Builder(this); 
       alertbox.setMessage("This is the alertbox!"); 
       alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 


        public void onClick(DialogInterface arg0, int arg1) { 
         Toast.makeText(getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_SHORT).show(); 
     stopService(new Intent(this, BackServices.class)); 
           finish(); 
        } 
       }); 

       // set a negative/no button and create a listener 
       alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() { 


        public void onClick(DialogInterface arg0, int arg1) { 
         Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show(); 
        } 
       }); 

       alertbox.show(); 


     } catch (Exception e) { 
      // TODO: handle exception 
     } 
    } 
    return super.onKeyDown(keyCode, event); 
} 
+0

當我按下返回鍵,對話是在第二時刻將關閉應用程序,以及對話沒有按任意鍵 –

+0

我已經做了一點改變我的答案中。現在試試 –

+0

仍然沒有通過你的方法解決 –

0
Keep your code as it is...with adding finish() call in positive button click 
And override onBackPressed() method with doing nothin 

public void onBackPressed() { 

} 

public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if ((keyCode == KeyEvent.KEYCODE_BACK)) { 
     try { 
       AlertDialog.Builder alertbox = new AlertDialog.Builder(this); 
       alertbox.setMessage("This is the alertbox!"); 
       alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 


        public void onClick(DialogInterface arg0, int arg1) { 
         Toast.makeText(getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_SHORT).show(); 
     stopService(new Intent(this, BackServices.class)); 
           finish(); 
        } 
       }); 

       // set a negative/no button and create a listener 
       alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() { 


        public void onClick(DialogInterface arg0, int arg1) { 
         Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show(); 
        } 
       }); 

       alertbox.show(); 


     } catch (Exception e) { 
      // TODO: handle exception 
     } 
    } 
    return super.onKeyDown(keyCode, event); 
} 
0

更好的解決方案將是返回到true表明您已經消耗的事件。

public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if ((keyCode == KeyEvent.KEYCODE_BACK)) { 
     // do your stuff 
     .... 
     .... 
     return true; // control will return from here. super will not be called. 
    } 
    return super.onKeyDown(keyCode, event); 
} 
相關問題