从EditText字段获取数据

时间:2012-02-26 15:39:04

标签: java android

我使用自定义布局创建了一个包含多个AlertDialog字段的自定义EditText。现在问题是当用户点击时,不知道如何从这些EditText获取数据。这是 AlertDialog

    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" />
....
....

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分: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变量,例如:

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