ポジティブ ボタンとネガティブ ボタンを備えた AlertDialog があります。AlertDialog レイアウトには、EditText と 2 つのボタン (btnAdd1、btnAdd2) があります。ユーザーがボタン btnAdd1 または btnAdd2 をクリックすると、AlertDialog の EditText に同じテキストが追加されます (ただし、AlertDialog は閉じません)。これは AlertDialog で可能ですか、それともダイアログのみを使用する必要がありますか?
これは AlertDialog のレイアウト (R.layout.prompt) です。
<LinearLayout>
<EditText
android:id="@+id/userInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnAdd1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bla" />
<Button
android:id="@+id/btnAdd2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bla" />
</LinearLayout>
そして、これはソースコードです:
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.prompt, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptView);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//...
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertD = alertDialogBuilder.create();
alertD.show();
レイアウトから btnAdd1 と btnAdd2 にアクセスしたい。OnClickListener() をこれら 2 つのボタンに設定します。