将EditText添加到对话框中

时间:2011-12-22 05:46:26

标签: android

我有一个像图中所示的对话窗口,我要求在每个项目旁边添加一个EditText(以获取数量),任何可能的方法来实现这一目标......?

want to add EditText next to every item

我尝试创建单独的视图并将其设置为AlertDialog,但在底部获取单个edittext ..

是否有任何可能的方法可以在清单中的每个项目旁边添加EditText,因为这里的项目是从一个Array Items []填充,带

      DialogInterface.OnMultiChoiceClickListener itemsDialogListener = 
      new DialogInterface.OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            if(isChecked)
                selectedItems.add(Items[which]);
            else
                selectedItems.remove(Items[which]);
                                    StringBuilder stringBuilder = new StringBuilder();

                            for(CharSequence item : selectedItems)
                        stringBuilder.append(item + ",");

                            selectItems.setText(stringBuilder.toString());
        }
    };

AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater li=LayoutInflater.from(this);
View v1=li.inflate(R.layout.itementry, null);//itementry view has one EditText
builder.setTitle("Select Items");
builder.setMultiChoiceItems(colours, checkedItems, itemsDialogListener);
builder.setView(v1);    //By setting this line only one EditText is visible at 
                              the bottom of alert dialog.   
AlertDialog dialog = builder.create();
dialog.show();      

4 个答案:

答案 0 :(得分:3)

     final AlertDialog.Builder alert = new AlertDialog.Builder(this);
     final EditText input = new EditText(this);
     alert.setTitle("Enter the Number of Items");
     alert.setView(input);
     //final String s=input.getText().toString();
     alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
     {
      public void onClick(DialogInterface dialog, int whichButton) 
      {
        int i=arg2;
        values[i]= input.getText().toString().trim();
      }
     });
    alert.show();

答案 1 :(得分:2)

是的,您可以使用setContentView()方法设计自己的视图(xml布局)并将其设置为对话框。

例如:

 dialog.setContentView(R.layout.layoutdialog);

 dialog.setView(R.layout.layoutdialog)   // for Alert dialog

答案 2 :(得分:0)

是的,Paresh Mayani是对的。我和他说的一样,这样做了。就像在该布局中创建一个XML布局一样,放置Edittext,spinners,textviews等......

<?xml version="1.0" encoding="utf-8"?>

    

<EditText
    android:id="@+id/titleText1"
    android:layout_width="200px"
    android:layout_height="wrap_content"
    android:layout_x="115dp"
    android:layout_y="11dp"
    android:singleLine="true" >

    <requestFocus></requestFocus>
</EditText>
<EditText android:layout_height="wrap_content" android:inputType="textPersonName" android:id="@+id/personText2" android:layout_y="66dp" android:layout_width="200px" android:layout_x="115dp"></EditText>
</AbsoluteLayout>

并在alertDialog构建器中使用此布局。像

        AlertDialog.Builder builder = new AlertDialog.Builder(yourActivity.this);
        LayoutInflater li=LayoutInflater.from(yourActivity.this);
        View v1=li.inflate(R.layout.alertDialogLayout, null);
        builder.setIcon(android.R.drawable.ic_input_get);
        builder.setView(v1);
        builder.setTitle("Something like your opinion");

并且,也请参考这些链接

1)Custom AlertDialog

2)Custom AlertDialog - 2

答案 3 :(得分:0)

EditText edt = new EditText(this);
    edt.setFilters(new InputFilter[] { new InputFilter.LengthFilter(10)});
    AlertDialog.Bulider altBx = new AlertDialog.Builder(this);
    altBx.setTitle("My dialog");
    altBx.setMessage("Welcome, Please Enter your name");
    altBx.setIcon(R.drawable.logo);
    altBx.setView(edt);

    altBx.setPositiveButton("Ok", new DialogInterface.OnClickListener()
    {
      public void onClick(DialogInterface dialog, int which)
      {
          if(edt.getText().toString().length()!=0)
          {
              //do anything
          }
          else 
          {
            //any msg
          }
      }
    });
    altBx.setNeutralButton("Cancel", new DialogInterface.OnClickListener()
    {
      public void onClick(DialogInterface dialog, int which)
      {
          //any msg
      }
    });
  altBx.show();