2013-04-23 110 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

相關問題