如何在AlertDialog框中添加两个编辑文本字段或视图?

时间:2013-04-23 12:49:28

标签: android views android-edittext android-alertdialog

我想在警告对话框中添加两个编辑文本字段。就像解决方案听起来那么简单,我还没有能够收集一个工作的。我无法同时设置两个(编辑文本)视图。

如果您想查看更多代码,请发表评论。

                alertDialog.setTitle("Values");
                final EditText quantity = new EditText(SecondScan.this);
                final EditText lot = new EditText(SecondScan.this);

                quantity.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                lot.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

                Project=arr[0].toString();
                Item=arr[1].toString();


                alertDialog.setMessage( "Employee No. : " + (Login.user).trim()+
                        "\nWarehouse      : " + (FirstScan.Warehouse).trim()+ 
                        "\nLocation           : " + (FirstScan.Location).trim()+ 
                        "\nProject              : " + Project.trim() + 
                        "\nItem                   : " + Item.trim() + 
                        "\nLot                      : " + Lot.trim()+  
                        "\n\nQuantity   :" );
                alertDialog.setView(quantity);
                    alertDialog.setView(lot);
 // the bit of code that doesn't seem to be working.


                alertDialog.setCancelable(false);
                alertDialog.setPositiveButton("Update",  new DialogInterface.OnClickListener() { 

                    public void onClick(DialogInterface dialog, int id) {
                        //ACTION
                    }
                });

                AlertDialog alert = alertDialog.create();
                alert.show();

我希望第一个编辑文字发生在批次之后,第二个编辑文本发生在数量之后,而当我尝试同时推送它们时,其中只有一个似乎正在工作意见。

UPDATE 事实证明,实际上没有方法可以单独向警告对话框添加多个视图,而无需为其创建布局。

4 个答案:

答案 0 :(得分:26)

请参阅android中的Creating a Custom Layout

enter image description here

修改

alertDialog.setTitle("Values");
final EditText quantity = new EditText(SecondScan.this);
final EditText lot = new EditText(SecondScan.this);

quantity.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
lot.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

Project=arr[0].toString();
Item=arr[1].toString();

LinearLayout ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(quantity);
ll.addView(lot);
alertDialog.setView(ll);

alertDialog.setCancelable(false);
alertDialog.setPositiveButton("Update",  new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        //ACTION
    }
});

AlertDialog alert = alertDialog.create();
alert.show();

答案 1 :(得分:10)

我使用LinearLayout登录弹出窗口:

public final String POPUP_LOGIN_TITLE="Sign In";
public final String POPUP_LOGIN_TEXT="Please fill in your credentials";
public final String EMAIL_HINT="--Email--";
public final String PASSWORD_HINT="--Password--";

AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle(POPUP_LOGIN_TITLE);
        alert.setMessage(POPUP_LOGIN_TEXT);

        // Set an EditText view to get user input 
        final EditText email = new EditText(this);
        email.setHint(EMAIL_HINT);
        final EditText password = new EditText(this);
        password.setHint(PASSWORD_HINT);
        LinearLayout layout = new LinearLayout(getApplicationContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(email);
        layout.addView(password);
        alert.setView(layout);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

          // Do something with value!
          }
        });

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

        alert.show();

答案 2 :(得分:2)

您应该创建一个垂直LinearLayout,您可以在其上添加EditTexts。然后使用alertDialog.setView()和LinearLayout。

在这里查看mor的信息:How to implement a custom AlertDialog View 或者How to add two edit text fields in an alert dialog

答案 3 :(得分:1)

为什么不为它制作完全自定义的布局?

这是一个自定义弹出窗口,用于显示类别列表并让用户选择一个。

public class CategoryPickerFragment extends DialogFragment implements OnItemClickListener{
private CategoryReceiver receiver;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    View view = inflater.inflate(R.layout.category_picker_fragment, null);

    builder.setView(view);
    AlertDialog ad = builder.create();

    CategoryList categoryList = (CategoryList) view.findViewById(R.id.clCategories);
    categoryList.setOnItemClickListener(this);

    return ad;
}
public void setCategoryReceiver(CategoryReceiver receiver){
    this.receiver = receiver;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Category category = ((CategoryListChild)view).getCategory();
    receiver.setCategory(category);
    this.dismiss();
}

请注意,我扩展了DialogFragment,使用自定义布局覆盖OnCreateDialog一个alertDialog,然后将其显示给用户。