2011-12-22 115 views
1

我有一個對話窗口,如圖所示,並且我有要求在每個項目旁邊添加一個EditText(取數量),任何可能的方法來實現此目的...?將EditText添加到對話框

want to add EditText next to every item

我試圖創建獨立的觀點,並將其設置爲AlertDialog,但得到的底部單一的EditText ..

有沒有辦法,我可以在清單中添加的EditText旁邊的每一個項目的任何可能的方式這裏BEC項目是從一個一個數組項[]填充,用

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

回答

2

是的,你可以設計自己的視圖(XML佈局),並通過使用setContentView()方法將其設置爲對話框。

例如:

dialog.setContentView(R.layout.layoutdialog); 

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

我想這應該是的setView(R.layout.layoutDialog) ; – 2011-12-22 06:00:13

+0

@ N-JOY選中此[Android自定義對話框](http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog)並搜索setContentView()。 – 2011-12-22 06:03:58

+0

你okk我以爲你提醒它alertDialog。因爲AlertDialog.Builder類具有方法setView(layout); – 2011-12-22 06:09:56

0

是,帕雷什Mayani是正確的。我已經做到了這一點,就像他所說的一樣。如創建在佈局一個XML佈局將您EDITTEXT,紡紗,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

+0

是的SPK,它的工作原理,但實際上我想添加edittext旁邊的每個項目,如果我設置builder.setView(視圖),我得到它在清單的底部, – user1065490 2011-12-22 06:17:23

+0

@ user1065490請參閱我編輯的答案並參考這些鏈接。這可能對您有所幫助。而且,不要忘記接受任何人的答案,你給出了準確的答案。 – Praveenkumar 2011-12-22 06:22:23

+0

是的SPK,它的工作原理,但實際上我想添加edittext旁邊的每個項目,如果我設置builder.setView(視圖),我得到它在清單的底部,實際上彈出窗口項目是從一個ArrayList ...我在做什麼,\t AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater li = LayoutInflater.from(this); View v1 = li.inflate(R.layout。itementry,null); builder.setTitle(「選擇項目」); builder.setMultiChoiceItems(colors,checkedItems,itemsDialogListener); \t \t builder.setView(v1); – user1065490 2011-12-22 06:27:58

2
 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(); 
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();