如何从自定义AlertDialog中获取edittext值,其中值的检索不在onClick中?

时间:2011-12-12 07:56:17

标签: android onclick android-edittext alertdialog

我要做的是创建一个覆盖AlertDialog的自定义对话框。 它应该做的是获取一些文本(至少2个字符串),然后对于每个字符串,它应该能够获得更多信息,但我想在自定义对话框中执行此操作。 那么应该发生的事情是用户可以在活动屏幕中输入2个人,然后对于第一个人,您会得到一个自定义对话框,该人可以输入三个单词,然后它会跳转到下一个自定义对话框(完全相同)布局我正在膨胀)第二个人可以输入一些单词。

这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinLay_Enter_Words"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
    android:id="@+id/TextView_AddPlayerWord_Instruction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="@dimen/help_text_size"
    android:textStyle="bold"></TextView>  
<EditText
    android:id="@+id/EditText_Word1"
    android:layout_height="wrap_content"
    android:maxLength="20"
    android:layout_width="match_parent"
    android:maxLines="1"></EditText>
<EditText
    android:id="@+id/EditText_Word2"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:maxLines="1"></EditText>
<EditText
    android:id="@+id/EditText_Word3"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:maxLines="1"></EditText>
</LinearLayout>

这是代码的一部分:

protected Dialog onCreateDialog(int id) {
    switch (id) {
    case  NOUN_INPUT_DIALOG_ID:
        Dialog returnedDialog = initWordDialog();
        return(returnedDialog);
    }
    return null; 
}

它调用initWordDialog():

private Dialog initWordDialog() {
    LayoutInflater inflater = LayoutInflater.from(this); //(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    final View dialogLayout = inflater.inflate(R.layout.word_entry_dialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    ...
    TextView v1 = (TextView) dialogLayout.findViewById(R.id.TextView_AddPlayerWord_Instruction);
    ...
v1.setText("SomeText");

    builder.setView(dialogLayout);
    builder.setTitle(R.string.enter_word_title);
    builder.setPositiveButton("Next", onNextSubmit);
    AlertDialog wordBuilderDialog = builder.create();
    return wordBuilderDialog;
}

我认为我想要找到的内容已经在某种程度上进行了讨论: Value of EditText in Custom Dialog Android - Custom Dialog - Can't get text from EditText How to add two edit text fields in an alert dialog 我相信这个问题就在这里,所有的例子都是每个人都有onClick与onCreate相同的功能。我的东西有点复杂,我想把功能分开;但是,因此,我现在无法访问任何EditText变量。

这是我的onClick实现:

private DialogInterface.OnClickListener onNextSubmit = new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        if (setPlayerWords()) {
        ...
    }
};

重要的部分是我甚至没有到达我正在访问edittexts的部分,直到调用setPlayerWords,这就是它失败的地方:

public boolean setPlayerWords() {
    PMGamePlay pmObj = (PMGamePlay) getApplicationContext();
    String[] playerWords = new String[pmObj.numberOfWordsPlayersGetToInput()];

    //LayoutInflater inflater = LayoutInflater.from(this);
    //View dialogLayout2 = inflater.inflate(R.layout.word_entry_dialog, null);
    //setContentView(R.layout.word_entry_dialog);
    final LinearLayout myLayout = (LinearLayout) findViewById(R.id.LinLay_Enter_Words);
    final EditText w0 = (EditText) myLayout.findViewById(R.id.EditText_Word1);
    final EditText w1 = (EditText) myLayout.findViewById(R.id.EditText_Word2);
    final EditText w2 = (EditText) myLayout.findViewById(R.id.EditText_Word3);
    String test = w0.getText().toString();

    playerWords[0] = w0.getText().toString();
    playerWords[1] = w1.getText().toString();
    playerWords[2] = w2.getText().toString();

    ...
    return true;
}

我最初尝试重新充气,但这似乎重置了,而edittexts不会为null,它们被重置为在其值中有“”。 然后我尝试在我的xml文件上设置了SessionContent,但这仍然给了我一个空值。 现在,我只是尝试简单地访问linearlayout,并返回null值。如果我只是尝试通过他们的id直接访问edittexts而不首先通过其parent linearlayout,它也返回一个空值。

在这一点上,我不知道除了将我在这些单独函数中拥有的所有东西塞进同一个单独的onclick之外该做什么,但我真的不想那样做。我可以做什么来访问这些edittexts吗?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用inflate inflate的长版本(XmlPullParser解析器,ViewGroup root,boolean attachToRoot)?我知道如果你不使用这种方法,它可能会遇到一些问题,它会抓住布局特征,因此可能会导致问题。对于视图组,您应该选择警报的parrent视图,并且通常需要attachToRoof = false;