Android基础TOP4_1:点击物理按钮弹出退出提示框

JAVA:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
    }
    //点击物理按钮时,执行退出提示
public boolean onKeyDown(int keyCode,KeyEvent event){
    if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){
        creatdialog();
    }
    return false;
    }
    

private void creatdialog() {
    // TODO Auto-generated method stub
    AlertDialog.Builder b=new Builder(MainActivity.this);
    //设置提示框内容
    b.setMessage("确认退出么");
    //设置标题栏
    b.setTitle("提示");
    b.setPositiveButton("确认",new OnClickListener() {
        
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            //点确定时退出程序,并隐藏此对话框
            dialog.dismiss();
            MainActivity.this.finish();
        }
    }).setNegativeButton("取消",new OnClickListener() {
        
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            //点击取消时只隐藏对话框
            dialog.dismiss();
        }
    });
    b.create().show();
    }

  
}

 

posted @ 2017-04-08 16:01  AndrioidCSY  阅读(517)  评论(0编辑  收藏  举报