简体   繁体   English

如何取消alertDialog中的按钮?

[英]How can I unsquash the buttons within my alertDialog?

I know I could solve this with a header but I'd rather not have one. 我知道我可以用标题解决此问题,但我宁愿没有标题。 Is there anyway to access the properties of the cancel button? 无论如何,有没有访问取消按钮的属性?

My code and an image of the AlertView is below. 我的代码和AlertView的图像如下。

在此处输入图片说明

for(int i = 0; i < 10; i++)
            {
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                if(view.getId() == (getResources().getIdentifier("imageButton" + (i+1), "id", "en.deco.android.livehud")))
                {


                    //alert.setTitle("Notes");

                    // Sets an EditText view to get user input 
                    final EditText input = new EditText(this);
                    input.setText(table.seats[i].getPlayer().getNotes());
                    alert.setView(input);

                    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) 
                    {
                        for(int i = 0; i < 10; i++)
                        {
                            if(view.getId() == (getResources().getIdentifier("imageButton" + (i+1), "id", "en.deco.android.livehud")))
                            {
                                table.seats[i].getPlayer().setNotes(input.getText().toString());
                            }
                        }

                    }
                    });

                    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {
                        // Canceled.
                      }
                    });
                    alert.show();
                    return true;
                }
            }

我认为您需要将EditText设置为定义的宽度,或fill_parent / match_parent

you can just set a view for your negative button, like this: 您可以为否定按钮设置视图,如下所示:

alert.setNegativeButton(R.id.cancelButton, new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Some action to be done when clicked..

            }
        });

and you can create a layout with a button as a root like this: 您可以创建一个以按钮为根的布局,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cancelButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Cancel">
</Button>

where you will set the width of the button normally to wrap_content 您通常将按钮的宽度设置为wrap_content

Edit : there is also a AlertDialog.Builder setNegativeButton (int textId, DialogInterface.OnClickListener listener) 编辑 :还有一个AlertDialog.Builder setNegativeButton(int textId,DialogInterface.OnClickListener侦听器)

setMinimumWidth() does the job. setMinimumWidth()完成这项工作。 The code above it is simply to generate display pixels rather than pixels. 上面的代码只是生成显示像素而不是像素。

DisplayMetrics metrics = getResources().getDisplayMetrics();
                        float dp = 250f;
                        int pixels = (int) (metrics.density * dp + 0.5f);
                        input.setMinimumWidth(pixels);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM