Refresh or change the AlertDialog Message

后端 未结 4 426
攒了一身酷
攒了一身酷 2021-01-11 09:32

I create an AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(this);
...
AlertDialog alert = builder.create();
alert.show();
<
相关标签:
4条回答
  • 2021-01-11 09:52

    Agreed with android developer. You can also use

    TextView messageView = (TextView) alert.findViewById(android.R.id.message);
    

    To get the control over the messageTextView of AlertDialog. Then you can set the new text there.

    0 讨论(0)
  • 2021-01-11 10:02

    Set OnClickListener out of main setter like this:

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity())
                            .setTitle(getResources().getString(R.string.dialog_title))
                            .setMessage("Waiting ...")
                            .setNeutralButton(R.string.check, null);
    
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
    
    Button b_neutral = alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL);
    b_neutral.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            TextView tv_message = (TextView) alertDialog.findViewById(android.R.id.message);
                            tv_message.setText("Works ...");
        }
    });
    
    0 讨论(0)
  • 2021-01-11 10:06

    Use alert.setMessage() instead of builder.setMessage(). Call alert.setMessage() and set message of your dialog anytime you want.

    Example:

         AlertDialog.Builder dialogBuilder;
         AlertDialog alertDialog;
    
         @Override
         protected void onCreate(Bundle savedInstanceState)
         {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
    
           dialogBuilder = new AlertDialog.Builder(MainActivity.this);
           alertDialog = dialogBuilder.create();
         }
    
    
         public void showAlert(int caller) {
           if(alertDialog != null && !alertDialog.isShowing()) {
            switch (caller){
                case 1:
                    alertDialog.setMessage("First method call");
                    break;
                case 2:
                    alertDialog.setMessage("Second method call");
                    break;
                case 3:
                    alertDialog.setMessage("Third method call");
                    break;
                }
                alertDialog.show();
             }
          }
    
    0 讨论(0)
  • 2021-01-11 10:15

    Yes ,you can.

    For example, if you create your own dialog, with your own layout, you can set an id for each of the views, and then access each of them (for example the textView) and set its new text whenever you wish to.

    0 讨论(0)
提交回复
热议问题