且构网

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

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

更新时间:2023-01-23 21:02:38

不要以您的方式设置消息.

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