意外行为onclick

时间:2014-09-02 06:37:32

标签: android android-alertdialog

我使用过AlertDialog,点击按钮进入主屏幕。我没有得到警报箱?

 alertDialog.setPositiveButton(R.id.button1, new 
    DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Toast.makeText(getActivity(), "save me**********************", Toast.LENGTH_SHORT).show();
            Toast.makeText(getActivity(), "you have pressed save", Toast.LENGTH_SHORT).show();

        }
    });

alertDialog.setNegativeButton("cancel", new DialogInterface.OnClickListener() {

1 个答案:

答案 0 :(得分:0)

试试这个

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setTitle(Your Title);

alertDialogBuilder.setMessage(Your Message);

// set positive button: Yes message

alertDialogBuilder.setPositiveButton(R.id.button1,new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog,int id) {

        //Your Code Here
    }
});

// set negative button: No message

alertDialogBuilder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog,int id) {

        // cancel the alert box and put a Toast to the user
        dialog.cancel();

        //Your Code Here

    }

});
AlertDialog alertDialog = alertDialogBuilder.create();

// show alert

alertDialog.show();