AlertDialog 中的复选框被“推出”通过文本视图

发布于 2024-11-29 19:10:32 字数 1319 浏览 1 评论 0原文

我正在尝试创建一个 AlertDialog 以在我的应用程序中显示介绍消息,其下方有一个“不再显示此消息”复选框。

当 AlertDialog 的消息很短时,它效果很好,但是当太长(需要滚动)时,复选框将不再显示。它被 TextView“推出”。

自定义视图的 XML (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>

以及显示 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();

关于如何解决这个问题有什么想法吗?也许有人有一个工作警报对话框的示例,其中带有“不再显示此”复选框?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜兔 2024-12-06 19:10:32

不要设置对话框消息,而是将其作为 textView 包含在布局 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">

<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>

Don't set the dialog box message, 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>
夜巴黎 2024-12-06 19:10:32

使用前面答案中的 XML 布局。

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文