Android 如何从自定义AlertDialog中获取edittext值,在该对话框中,值的检索不在onClick中?
我试图做的是创建一个覆盖AlertDialog的自定义对话框。 它应该做的是获取一些文本(至少2个字符串),然后对于这些字符串中的每一个,它应该能够获取更多信息,但我想在自定义对话框中这样做。 因此,假设一个用户可以在活动屏幕中输入两个人,然后第一个人可以得到一个自定义对话框,这个人可以输入三个单词,然后跳转到下一个自定义对话框(完全相同的布局,我正在膨胀),第二个人可以输入一些单词 这是我的xml:Android 如何从自定义AlertDialog中获取edittext值,在该对话框中,值的检索不在onClick中?,android,onclick,android-edittext,android-alertdialog,Android,Onclick,Android Edittext,Android Alertdialog,我试图做的是创建一个覆盖AlertDialog的自定义对话框。 它应该做的是获取一些文本(至少2个字符串),然后对于这些字符串中的每一个,它应该能够获取更多信息,但我想在自定义对话框中这样做。 因此,假设一个用户可以在活动屏幕中输入两个人,然后第一个人可以得到一个自定义对话框,这个人可以输入三个单词,然后跳转到下一个自定义对话框(完全相同的布局,我正在膨胀),第二个人可以输入一些单词 这是我的xml: <?xml version="1.0" encoding="utf-8"?> &l
<?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>
它调用initWordDialog():
我想我试图找到的东西已经在某种程度上被讨论过了:
我相信问题就在这里,所有的例子都有他们的onClick和onCreate在同一个函数中。我的东西有点复杂,我想把功能分开;但是,因此,我现在无法访问任何EditText变量
以下是我的onClick实现:
private DialogInterface.OnClickListener onNextSubmit = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (setPlayerWords()) {
...
}
};
重要的是,在调用setPlayerWords之前,我甚至无法访问EditText,这就是它失败的地方:
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;
}
我最初尝试重新膨胀,但似乎重置了,虽然EditText不会为空,但它们的值中重置为“”。
然后,我尝试在xml文件上设置ContentView,但仍然给我一个空值。
现在,我只是尝试访问linearlayout,它还返回一个空值。如果我只是尝试通过EditText的id直接访问它们,而不首先通过其父linearlayout,那么它也会返回空值
在这一点上,除了将这些单独函数中的所有内容都塞进同一个onclick之外,我不知道该做什么,但我真的不想这样做。我能做些什么来访问这些编辑文本吗
任何帮助都将不胜感激 您是否尝试过使用长版本的充气充气(XmlPullParser解析器、ViewGroup根、boolean attachToRoot)?我知道,如果不使用此方法,在获取布局特征时可能会出现一些问题,因此可能是导致问题的原因。对于视图组,您应该为警报选择parrent视图,并且通常希望attachToRoof=false 对每个编辑文本使用不同的ID(在整个应用程序中是唯一的)。这是我在整个应用程序中唯一有编辑文本的地方。另一次我在活动中有输入,我以编程方式添加了编辑文本。我只是尝试了一下。LayoutFlater充气机=LayoutFlater.from(本机);视图对话框Layout2=充气机。充气(R.layout.word\u entry\u dialog,(视图组)findViewById(R.id.LinLay\u Enter\u Words),false);但它似乎不起作用,您作为父R.id.LinLay\u输入单词传递的布局是什么。试着传递活动上下文,我想这是一个对话框的父对象,但我猜这里有一点。
private DialogInterface.OnClickListener onNextSubmit = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (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;
}