Checkbox in AlertDialog “pushed out” by TextView

淺唱寂寞╮ 提交于 2020-01-04 05:13:09

问题


I'm trying to create an AlertDialog to show an introduction message in my application, with a "Don't show this again" CheckBox below it.

It works well when the message of the AlertDialog is short, but when is too long (requiring scrolling) the CheckBox is no longer shown. It gets "pushed out" by the TextView.

The XML for the custom view (dont_show_again.xml):

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

    <CheckBox 
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="Don't show this again">
    </CheckBox>
</LinearLayout>

And the code to display the AlertDialog

String longMessage = getString(R.string.long_message);

LayoutInflater inflater = getLayoutInflater();
final View checkboxLayout = inflater.inflate(R.layout.dont_show_again, null);

AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("Some message")
.setMessage(longMessage)
.setView(checkboxLayout)
.setPositiveButton("Ok",
        new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
});

AlertDialog dialog = builder.create();
dialog.show();

Any ideas on how to solve this? Perhaps someone has an example of a working AlertDialog with a "Don't show this again" CheckBox?


回答1:


Dont set the dialog box messsage instead include it in the layout xml as textView.

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

<TextView
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"    
    android:singleLine="false"
    android:maxLines="3"
    android:scrollbars="vertical"/>
<CheckBox 
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Don't show this again">
</CheckBox> 
</LinearLayout>



回答2:


Use the XML layout from the answer earlier.

String longMessage = getString(R.string.long_message);
LayoutInflater inflater = getLayoutInflater();
final View checkboxLayout = inflater.inflate(R.layout.dont_show_again, null);
CheckBox cb = (CheckBox)checkboxLayout.findViewById(R.id.checkBox);
TextView tv = (TextView)checkboxLayout.findViewById(R.id.message);
tv.setText(longMessage);
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("Some message")
.setView(checkboxLayout)
.setPositiveButton("Ok",
    new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
    }
});
AlertDialog dialog = builder.create();
dialog.show();


来源:https://stackoverflow.com/questions/7049228/checkbox-in-alertdialog-pushed-out-by-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!