且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android 在警报对话框中设置文本

更新时间:2022-01-21 02:19:21

不要用你的方式设置消息.

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

在您的自定义布局中,您必须设置的 textview 是dialogtext"TextView.试试这个……看看我得到了我膨胀的自定义视图,从那个视图中我得到了自定义对话框(你之前实际上没有设置)并设置消息在构建发生后显示.实际上,您可以拥有任何您想要的自定义视图,并根据自己的喜好设置视图的每个元素,甚至可以处理其上的事件

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();