How do I get edittext value from a custom AlertDia

2019-05-29 19:51发布

What I am trying to do is to create a custom dialog that overrides an AlertDialog. What it is supposed to do is get some text (at least 2 strings) and then for each of those strings it is supposed to be able to get more information, but I want to do this in custom dialogs. So what is supposed to happen is a user can enter 2 people in an activity screen, and then for the first person, you get a custom dialog and that person can enter three words, and then it jumps to the next custom dialog (exact same layout I am inflating) and the second person can enter some words.

This is my 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>

And this is part of the code:

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

It calls 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;
}

I think what I am trying to find has been discussed to some degree here: 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 The problem, I believe, lies here, where all of the examples everyone has their onClick in the same function as their onCreate. My stuff was a bit more complicated and I wanted to separate out the functions; however, as a result, I am now unable to access any of the EditText variables.

Here is my onClick implementation:

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

The part that matters is I don't even get to the part where I'm accessing the edittexts until setPlayerWords is called, and this is where it is failing:

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;
}

I initially tried re-inflating, but that seemed to reset and while the edittexts would not be null, they were reset to have "" in their values. Then I tried to setContentView on my xml file, but that still gave me a null value. Now, I just try and simply access the linearlayout, and that also returns a null value. If I just try to access the edittexts by their id directly without first going through its parent linearlayout, it also returns a null value.

At this point, I'm not sure what to do other than to cram everything that I have in these separate functions into the same single onclick, but I really don't want to do that. Is there nothing else I can do to access these edittexts?

Any help would be greatly appreciated!

1条回答
霸刀☆藐视天下
2楼-- · 2019-05-29 20:34

Have you tried using the long version of inflate inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) ? I know that if you don't use this method there can be some issues with it grabbing layout characteristics, so might be causing the issue. For the viewgroup you should pick the parrent view for the alert and usually want attachToRoof = false;

查看更多
登录 后发表回答