再次使用新消息值显示对话框

时间:2016-01-26 16:38:23

标签: java android

有没有办法显示相同的对话但具有不同的值?

 public void onClick(View view) {
      Calendar currentTime = Calendar.getInstance();
      final int hour = currentTime.get(Calendar.HOUR_OF_DAY);
      final int minute = currentTime.get(Calendar.MINUTE);
      final String message = "Right now is" + hour + minute;

      //Creates a dialog with a message about the current time
      new AlertDialog.Builder(getContext())
           .setTitle("ATENTION:")
           .setMessage(message)
           .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //Do Something
            } })

           .setNegativeButton("Change Time", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                 //Shows a new dialog to pick time
                 TimePickerDialog timePicker = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
                 @Override
                 public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                      int hour = selectedHour;
                      int minute = selectedMinute;
                 } }, hour, minute, true);
                 timePicker.show();
            } })

            .setIcon(android.R.drawable.sym_def_app_icon)
            .show();
        }

在timePicker对话框中单击“确认”后,我想再次显示第一个对话框(带有消息的对话框),但现在选择了新的时间。这可能吗?

2 个答案:

答案 0 :(得分:1)

您可以使用返回对话框对象的函数,然后将消息传递给函数并调用show。

public AlertDialog getDialog(String message){
    new AlertDialog.Builder(getContext())
           .setTitle("ATENTION:")
           .setMessage(message)
           .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //Do Something
            } })

           .setNegativeButton("Change Time", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                 //Shows a new dialog to pick time
                 TimePickerDialog timePicker = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
                 @Override
                 public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                      int hour = selectedHour;
                      int minute = selectedMinute;
                 } }, hour, minute, true);
                 timePicker.show();
                 } })
            .setIcon(android.R.drawable.sym_def_app_icon)
            .show();
}

getDialog("message").show();

答案 1 :(得分:1)

是的,您可以将对话框保存为字段,例如AlertDialog mDialog; 当你想要显示它时,首先检查它是否为null,如果为null则使用builder.build来初始化它。另外使用mDialog.setMessage(<new message>)

 ALertDialog mDialog = null;
 public void onClick(View view) {
    if(mDialog == null){
        mDialog = newDialog();
    }else{
         final int hour = currentTime.get(Calendar.HOUR_OF_DAY);
         final int minute = currentTime.get(Calendar.MINUTE);
         final String message = "Right now is" + hour + minute;
         mDialog.setMessage(message);
    }
    mDialog.show();
 }

 public AlertDialog newDialog(){
 Calendar currentTime = Calendar.getInstance();
  final int hour = currentTime.get(Calendar.HOUR_OF_DAY);
  final int minute = currentTime.get(Calendar.MINUTE);
  final String message = "Right now is" + hour + minute;

  //Creates a dialog with a message about the current time
  return new AlertDialog.Builder(getContext())
       .setTitle("ATENTION:")
       .setMessage(message)
       .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            //Do Something
        } })

       .setNegativeButton("Change Time", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
             //Shows a new dialog to pick time
             TimePickerDialog timePicker = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
             @Override
             public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                  int hour = selectedHour;
                  int minute = selectedMinute;
             } }, hour, minute, true);
             timePicker.show();
        } })

        .setIcon(android.R.drawable.sym_def_app_icon)
        .build();
 }