2011-09-07 70 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); 
} 
相关问题