如何使用中性按钮创建自定义首选项?

发布于 2024-11-04 18:11:28 字数 1039 浏览 1 评论 0原文

我使用下面的代码来创建自定义首选项。 xml 布局文件有 ButtonEditTextTextView。此自定义布局出现在带有“确定”和“取消”按钮的警报内。这一切都运作良好。

我想在“确定”和“取消”按钮旁边添加第三个按钮(中性按钮)。我已经尝试过 AlertBuilder 类,但不知道如何合并我的自定义xml 布局和中性按钮

如何做到?

目前

public class MelsMessage extends DialogPreference {

    Button bMessage;
    EditText eMessage;
    TextView tMessage;

    public MelsMessage(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }



    protected View onCreateDialogView() {

        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        View view = layoutInflater.inflate(R.layout.dialog_pref_mess, null);

        //UI elements

        bMessage = (Button) view.findViewById(R.id.buttonMessage);
        eMessage = (EditText) view.findViewById(R.id.edittextMessage);
        tMessage = (TextView) view.findViewById(R.id.textviewMessage);


        return view;        
    }

}

I use the code below to create a custom preference. The xml layout file has a Button, EditText and TextView. This custom layout appears inside an Alert with "OK" and "Cancel" buttons. This all works well.

I would like to add a third button (a neutral button) beside the "OK and "Cancel" buttons. I've experimented with the AlertBuilder class but can't figure out how to incorporate both my custom xml layout and a neutral button.

How can this be done?

Currently have...

public class MelsMessage extends DialogPreference {

    Button bMessage;
    EditText eMessage;
    TextView tMessage;

    public MelsMessage(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }



    protected View onCreateDialogView() {

        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        View view = layoutInflater.inflate(R.layout.dialog_pref_mess, null);

        //UI elements

        bMessage = (Button) view.findViewById(R.id.buttonMessage);
        eMessage = (EditText) view.findViewById(R.id.edittextMessage);
        tMessage = (TextView) view.findViewById(R.id.textviewMessage);


        return view;        
    }

}

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

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

发布评论

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

评论(3

欲拥i 2024-11-11 18:11:28

我发现您的问题有点老了,也许您已经有了问题的答案,但这里有一个扩展 DialogPreference 的类的解决方案。

首先,您必须重写 MelsMessage 类中的 onPrepareDialogBu​​ilder 方法:

@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder)
{
    super.onPrepareDialogBuilder(builder);
    builder.setNeutralButton("hello", this);
}

setNeutralButton 中的 this code> 方法是 DialogPreference 类实现的 DialogInterface.OnClickListener 接口。

您要做的最后一件事是覆盖 MelsMessage 类中的 onClick 方法:

@Override
public void onClick(DialogInterface dialog, int which)
{
    super.onClick(dialog, which);

    switch (which)
    {
        case DialogInterface.BUTTON_POSITIVE:
            // do things for the right button
            break;

        case DialogInterface.BUTTON_NEGATIVE:
            // do things for the left button
            break;

        default:
            // do things for the center button
            break;
    }
}

如果您想在另一个类中处理所有点击你所要做的就是在这个类中实现DialogInterface.OnClickListener

希望这会对您有所帮助。干杯。

I see your question is a bit old and maybe you already have the answer to your question but here is a solution for your class that extends DialogPreference.

First you have to Override the onPrepareDialogBuilder method in your MelsMessage class:

@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder)
{
    super.onPrepareDialogBuilder(builder);
    builder.setNeutralButton("hello", this);
}

this in the setNeutralButton method is DialogInterface.OnClickListener interface that the DialogPreference class implement.

The last thing you have to do is to Override the onClick method in your MelsMessage class:

@Override
public void onClick(DialogInterface dialog, int which)
{
    super.onClick(dialog, which);

    switch (which)
    {
        case DialogInterface.BUTTON_POSITIVE:
            // do things for the right button
            break;

        case DialogInterface.BUTTON_NEGATIVE:
            // do things for the left button
            break;

        default:
            // do things for the center button
            break;
    }
}

If you want to handle the click in another class all you have to do is to implement DialogInterface.OnClickListener in this class.

Hope this will help you. Cheers.

半世蒼涼 2024-11-11 18:11:28

当您重写 onCreateDialogView 且未设置 setPositiveButton 和 setNegativeButton 时,“确定”和“取消”按钮应该消失,不是吗?因为在该方法中,您将覆盖默认布局并设置自定义布局。

如果是这种情况,那么您应该创建自己的自定义底部布局,其中包含 3 个按钮,并将其添加到膨胀的布局中。尝试搜索并实现“底部 ButtonBar”,它具有所有必要的实现,因为我在文档中没有看到任何方法或方式来实现像常规对话框一样的中性按钮。

When you are overriding onCreateDialogView and you don't set the setPositiveButton and setNegativeButton the ok and cancel button should disappear, don't they? Because in that method you are overriding the default layout and setting a custom one.

If that is the case then you should create you own custom bottom layout with 3 buttons and add it in the inflated one. Try searching and implementing the "bottom ButtonBar" which has all the necessary implementation, because I don't see in the docs any method or a way to implement the neutral button like on a regular Dialog.

忆悲凉 2024-11-11 18:11:28

您可以创建带有 3 个按钮的自定义对话框或编写如下代码

Dialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int id) {



    } }); 

 Dialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancle", new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int id) {



    }}); 

 Dialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Do Nothing" new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int id) {


    }});

you can create custom Dialog with 3 buttons or write code like

Dialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int id) {



    } }); 

 Dialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancle", new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int id) {



    }}); 

 Dialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Do Nothing" new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int id) {


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