私がしようとしているのは、AlertDialogをオーバーライドするカスタムダイアログを作成することです。 これは何らかのテキスト(少なくとも2つの文字列)を取得して、それぞれの文字列に対してより多くの情報を取得するはずですが、カスタムダイアログでこれを実行したいと考えています。 これは、ユーザーが2人のユーザーをアクティビティ画面に入力し、最初のユーザーに対してカスタムダイアログを表示し、そのユーザーが3単語を入力した後、次のカスタムダイアログに移動するというものです同じレイアウト私は膨張している)と2番目の人はいくつかの単語を入力することができます。値の取得がonClickにないカスタムAlertDialogからedittext値を取得するにはどうすればよいですか?
これは私の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()) {
...
}
};
部分は私もsetPlayerWordsが呼び出されるまで、私はedittextsにアクセスしてる部分を取得していない、それが失敗した場合、これはです:
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ファイルにsetContentViewを作成しようとしましたが、それでもnull値が返されました。 これで、単純に線図レイアウトにアクセスしようとしましたが、これもnull値を返します。私は、親の線形描画を最初に通過することなく、そのidで直接編集テキストにアクセスしようとすると、null値も返します。
この時点で、私はこれらの別々の機能で持っているすべてを同じ単一のonclickに詰め込む以外に何をすべきかはわかりませんが、実際にはそうしたくありません。これらの編集テキストにアクセスするために他に何もできませんか?
ご協力いただければ幸いです!
編集テキストごとに異なるIDを使用します(アプリ全体で一意です)。 – Noby
これは私がアプリケーション全体でedittextを持っている唯一の場所です。別の時間に私はアクティビティに入力を与えて、プログラムでエディットテキストを追加しました。 – user1031885