关于自定义对话框

发布于 2024-12-18 06:06:06 字数 1029 浏览 2 评论 0原文

我应该列出可点击的项目。我暂时写了文字。我如何使它们可点击?抱歉,我刚刚开始在 Android 中编程。你能帮我看一下代码吗?

public class CustomDialog extends Dialog 
{
    public CustomDialog(Context context) 
    {
        super(context, android.R.style.Theme_Translucent_NoTitleBar);
    }

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.privacy_popup);

        TextView text = ((TextView)this.findViewById(R.id.text));
        text.setText(Html.fromHtml("<b>item<b>"));
        //text.setText("Privacy");

        TextView text2 = (TextView) findViewById(R.id.text2);
        text2.setText(Html.fromHtml("<b>item2<b>"));

        TextView text3 = (TextView) findViewById(R.id.text3);
        text3.setText(Html.fromHtml("<b>item3<b>"));

        TextView text4 = (TextView) findViewById(R.id.text4);
        text4.setText(Html.fromHtml("<b>item4<b>"));
    }
}

I should make a list of clickable items. I wrote text for now. How do I make them clickable? Sorry but I just started to program in Android. Can you help me with the code?

public class CustomDialog extends Dialog 
{
    public CustomDialog(Context context) 
    {
        super(context, android.R.style.Theme_Translucent_NoTitleBar);
    }

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.privacy_popup);

        TextView text = ((TextView)this.findViewById(R.id.text));
        text.setText(Html.fromHtml("<b>item<b>"));
        //text.setText("Privacy");

        TextView text2 = (TextView) findViewById(R.id.text2);
        text2.setText(Html.fromHtml("<b>item2<b>"));

        TextView text3 = (TextView) findViewById(R.id.text3);
        text3.setText(Html.fromHtml("<b>item3<b>"));

        TextView text4 = (TextView) findViewById(R.id.text4);
        text4.setText(Html.fromHtml("<b>item4<b>"));
    }
}

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

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

发布评论

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

评论(3

眼前雾蒙蒙 2024-12-25 06:06:06

好吧,首先我建议您使用 DialogFragment 如果可能的话,但无论如何它是扩展对话框真的很容易,您可能需要检查 自定义对话框 来自官方安卓指南。

Well firstable i would suggest you using DialogFragment if it possible, but anyway it's really easy to extends a Dialog, you might want to check Custom Dialog from the official Android guide.

神妖 2024-12-25 06:06:06

如果您被迫使用 TextView 元素,您可以实现 TextView onClickListener 侦听器,如下所示:

textView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO : do something

    }
});

If you are forced to use TextView elements, you can implement the TextView onClickListener listener as this:

textView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO : do something

    }
});
っ左 2024-12-25 06:06:06

您甚至不必扩展 Dialog 并创建自定义对话框; AlertDialog 提供您正在寻找的所有功能。下面只不过是对话框主题的复制粘贴在开发者网站上。

final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(), 
            items[item], Toast.LENGTH_SHORT).show();
    }
});
AlertDialog alert = builder.create();

这将导致:

AlertDialog Showing list of options

交换上面的 builder.setItems(...) for:

builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { ... } );

为每个选项添加单选按钮,或者:

builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() { ... } );

用于复选框。

AlertDialog 显示单选选项列表(带有单选按钮)

You don't even have to extend Dialog and create a custom dialog; an AlertDialog provides all the functionality you're looking for. Below is nothing more than a copy paste from the Dialog topic on the developer website.

final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(), 
            items[item], Toast.LENGTH_SHORT).show();
    }
});
AlertDialog alert = builder.create();

This will result in:

AlertDialog showing list of options

Swap out above builder.setItems(...) for:

builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { ... } );

to add a radio button with each option, or:

builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() { ... } );

for checkboxes.

AlertDialog showing list of single choice options (with radio button)

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