【问题标题】:Android Set text in alert dialogAndroid 在警报对话框中设置文本
【发布时间】:2014-12-15 23:35:41
【问题描述】:

我正在尝试创建一个警报对话框,顶部有一个图像标题,底部有 2 个按钮,我想通过代码在中间设置文本。但是,我无法在横幅下方设置文本。我想创建类似于this link 的东西。但是,文本总是在图像上方结束,因为我不知道如何在对话框构建器中引用文本视图。谢谢

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.custom_dialog, null))
           .setMessage("Message" + message)
           .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) {}
           });

    AlertDialog alert = builder.create();

    alert.setOnDismissListener(new OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {}
    });

    alert.show();

布局xml文件为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
    android:src="@drawable/yo3"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:scaleType="center"
    android:background="#FFFFBB33"
    android:contentDescription="@string/app_name" />

<TextView
    android:id="@+id/dialogtext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>`

【问题讨论】:

  • 为什么不在布局文件中添加对话框文本?

标签: android android-layout layout dialog android-alertdialog


【解决方案1】:

不要以你的方式设置信息。

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

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

【讨论】:

    猜你喜欢
    • 2013-09-17
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    相关资源
    最近更新 更多