Android Set text in alert dialog

前端 未结 1 443
臣服心动
臣服心动 2020-12-22 04:37

I\'m trying to create an alert dialog with an image header across the top, 2 buttons at the bottom and I want to set the text in the middle from code. However

相关标签:
1条回答
  • 2020-12-22 05:01

    Don't set the message with the way you do it.

    In your custom layout the textview you have to set is the "dialogtext" TextView. Try this...see that i get the custom view that i inflate and from that view i get the custom dialog (which you didn't actualy set before) and set the message to show after the build has taken place. Actually you can have whatever custom view you want and set each element of the view as you prefer or even handle events on it

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = getLayoutInflater();
    
    View customView = inflater.inflate(R.layout.custom_dialog, null);
    TextView messageView = (TextView)customView.findViewById(R.id.dialogtext);
    
    
    builder.setView(customView)
            .setPositiveButton("Share", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
    
                   if (isNetworkAvailable()){
    
                        if (......;
    
                        } else {
                            ......
                        }
    
                   } else {
                       .......;
                   }    
               }  
           })
           .setNegativeButton("Shake again!", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
    
               }
    
        }).setOnCancelListener(new OnCancelListener() {
    
            @Override
            public void onCancel(DialogInterface dialog) {
    
            }
        });
    
    messageView.SetText("Message" + message);
    
    
    
    AlertDialog alert = builder.create();
    
    alert.setOnDismissListener(new OnDismissListener() {
    
        @Override
        public void onDismiss(DialogInterface dialog) {
    
    
        }
    });
    alert.show();
    
    0 讨论(0)
提交回复
热议问题