2012-03-28 29 views
0

我知道我可以用头来解决这个问题,但我宁愿没有。无论如何访问取消按钮的属性?如何解开alertDialog中的按钮?

我的代码和AlertView的图像如下。

enter image description here

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

回答

0

setMinimumWidth()完成这项工作。上面的代码只是生成显示像素而不是像素。

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

我想你会需要你的EditText设置为定义的宽度,或fill_parent/match_parent

+0

怎么样?在EditText文档中,我看不到任何允许这样做的内容。 – Deco 2012-03-29 17:31:45

+1

将新的LayoutParams对象分配给编辑文本。使用充气的XML布局而不是一个View可能更容易。 – Phix 2012-03-30 09:32:50

+0

我给它一个去,Fill Parent和Wrap_Content没有效果(内容已经被默认包装了?)。设置为固定大小的工作,但我希望宽度是可扩展的,所以我最终的解决方案对我来说更好。非常感谢。 – Deco 2012-03-30 14:15:03

0

你可以设置视图为您的否定按钮,像这样:

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

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

      } 
     }); 

,你可以创建一个按钮布局这样一个根:

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

在那里你会设置按钮的宽度通常为wrap_content

编辑:还有一个 AlertDialog.Builder setNegativeButton (int textId, DialogInterface.OnClickListener listener)

+0

我不认为这有效。 setNegativeButton不会将我们的customView设置为negativeButton。第一个参数仅用于在按钮上显示文本。 [链接] http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setNegativeButton(java.lang.CharSequence,android.content.DialogInterface.OnClickListener) 当试图取消被取代通过假而按钮仍然有两行。有没有其他方法可以将我们自己的视图设置为否定按钮? – Deco 2012-03-29 16:58:27