2012-02-26 83 views
1

我使用自定義佈局創建了具有多個EditText字段的自定義AlertDialog。現在的問題是當用戶點擊時不知道如何從這些EditText獲取數據。這裏是AlertDialog從EditText字段獲取數據

case R.id.menu_newitem: 
     LayoutInflater factory = LayoutInflater.from(this); 
     final View textEntryView = factory.inflate(R.layout.newvslayout, null); 
     new AlertDialog.Builder(this).setTitle("New VS") 
       .setView(textEntryView).setPositiveButton("Ok", new DialogInterface.OnClickListener(){ 
        public void onClick(DialogInterface dialog, int whichButton) { 

         /* User clicked OK so do some stuff */ 
        } 
       }).setNeutralButton("Cancel", new DialogInterface.OnClickListener(){ 
        public void onClick(DialogInterface dialog, int whichButton) { 

         /* User clicked cancel so do some stuff */ 
        } 
       }).show(); 

這是我的佈局newvslayout.xml

.... 
.... 
<EditText android:id="@+id/vsname" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:text="Name" 
    android:inputType="text" /> 

<EditText android:id="@+id/csize" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
      android:text="100" 
    android:inputType="number" /> 

<EditText android:id="@+id/dsize" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="600" android:inputType="number" /> 

<EditText android:id="@+id/ssize" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="200" android:inputType="number" /> 
.... 
.... 
+0

看到這個[鏈接]( http://stackoverflow.com/a/7200155/990029)。 – Deepak 2012-02-26 16:13:06

回答

3
edittext t1 = (edittext) textEntryView.findviewbyid(R.id.vsname); 
String s = t1.gettext().toString(); 
1

讓我上user1027958的回答擴大:

首先,我們需要得到的EditText組件的保持:

EditText et_vsName = (EditText) textEntryView.findviewbyid(R.id.vsname); 
EditText et_cSize = (EditText) textEntryView.findviewbyid(R.id.csize); 
EditText et_dSize = (EditText) textEntryView.findviewbyid(R.id.dsize); 
EditText et_sSize = (EditText) textEntryView.findviewbyid(R.id.ssize); 

然後我們就可以進去每個的EditText框的文本到字符串變量,例如:

String vsName = et_vsName.getText().toString(); 

完成的代碼可能會是這樣的:

case R.id.menu_newitem: 
    LayoutInflater factory = LayoutInflater.from(this); 
    final View textEntryView = factory.inflate(R.layout.newvslayout, null); 
    final EditText et_vsName = (EditText) textEntryView.findviewbyid(R.id.vsname); 
    final EditText et_cSize = (EditText) textEntryView.findviewbyid(R.id.csize); 
    final EditText et_dSize = (EditText) textEntryView.findviewbyid(R.id.dsize); 
    final EditText et_sSize = (EditText) textEntryView.findviewbyid(R.id.ssize); 
    new AlertDialog.Builder(this).setTitle("New VS") 
      .setView(textEntryView).setPositiveButton("Ok", new DialogInterface.OnClickListener(){ 
       public void onClick(DialogInterface dialog, int whichButton) { 
        //Get the value of the EditText(s) 
        String vsName = et_vsName.getText().toString(); 
       } 
      }).setNeutralButton("Cancel", new DialogInterface.OnClickListener(){ 
       public void onClick(DialogInterface dialog, int whichButton) { 
        //Close the dialog 
        this.dismiss(); 
       } 
      }).show();