2013-04-23 113 views
11

我想在警报对话框中添加两个编辑文本字段。就像解决方案听起来一样简单,我还没有能够收集到一个工作。我无法同时设置两个(编辑文本)视图。如何在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事实证明其实也有不单独添加多个视图一个警告对话框,而不必为它创建一个布局的方法。

+0

两个视图创建布局,并设置作为AlertDialog内容 – Triode 2013-04-23 12:53:39

+0

您可以通过创建一个对话框(不带alertDialog.Builder)并设置一个在xml中定义的内容视图吨。所以,你得到你自己的自定义布局的对话框。 – Opiatefuchs 2013-04-23 12:54:37

+0

能否请您在答案中提供一些示例代码,以便我可以尝试并在他们解决问题时接受它们?谢谢。 – 2013-04-23 12:57:03

回答

23

在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(); 
+0

嘿,感谢上述!任何有关将视图添加到对话框而不必为其创建布局的解决方案? – 2013-04-23 13:22:17

+0

编辑请见 – 2013-04-23 13:31:29

+0

太棒了!只是我正在寻找的解决方案。另一个,如果你能帮助我,有没有一种方法可以将我的editText视图放置在textView的左侧? 例如:**批号**:**编辑文本字段** – 2013-04-24 03:51:45

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与自定义布局,然后将其展示给用户。

+0

感谢您的快速回复,但我一直沿着向警报添加视图的方向寻找某些东西,而不是编码布局。任何帮助都会很棒! – 2013-04-23 13:18:35

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(); 
+0

完美的作品! – 2016-04-02 12:18:12

相关问题