AlertDialogにEditTextを2つ付けることは可能でしょうか?

2,665 views
Skip to first unread message

mid-_-night

unread,
Jan 4, 2010, 9:41:30 AM1/4/10
to 日本Androidの会
件名の通りなのですが,AlertDialogにEditTextを2つ付けることは可能でしょうか?
現在,
setViewを用いて1つは配置することが出来たのですが,もう1つが付けることができません.
AlertDialogで不可能ならば,他に何か良い方法はないでしょうか?
入力として文字列を2つ入力させたいのですが.
どなたかよろしくお願いします.

Akio Kondo

unread,
Jan 4, 2010, 10:00:11 AM1/4/10
to 日本Androidの会
こんどうです。

Dialogクラスを使って自作レイアウトのダイアログを使ってはどうでしょうか。
自作のレイアウトなのでボタンはいくつでも追加できます。

---------------------
アクティビティ内のダイアログ表示のコード
---------------------
mDialog = new Dialog(MainActivity.this);
mDialog.setContentView(R.layout.mydialog);
mDialog.setTitle("Dialogのタイトル");
mDialog.findViewById(R.id.buttonOk).setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
//OKを押したときの処理
mDialog.dismiss();
}
}
);
mDialog.show();

---------------------
mydialog.xml
---------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/textDialogBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:text="ダイアログ本文"/>
<LinearLayout android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="OK"
android:id="@+id/buttonOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

Akio Kondo

unread,
Jan 4, 2010, 10:02:55 AM1/4/10
to 日本Androidの会
おっと、EditTextでしたね。

もちろん、ボタンと同様にEditTextも好きなだけ増やせます。
レイアウトにEditTextを追加しておいて
onClickの時にEditTextから値を取得するだけです。

CHEN,Zi-zhao

unread,
Jan 5, 2010, 1:48:49 AM1/5/10
to android-g...@googlegroups.com
陳です。

例のコード:
Builder dialog = new AlertDialog.Builder(this);
LinearLayout layout = new LinearLayout(this);
final EditText text1 = new EditText(this);
text1.setWidth(100);
final EditText text2 = new EditText(this);
text2.setWidth(100);
layout.addView(text1);
layout.addView(text2);
dialog.setView(layout);

dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String message = text1.getText().toString() + "\t"
+ text2.getText().toString();
Toast.makeText(getApplicationContext(), message,
Toast.LENGTH_SHORT).show();
}
});

dialog.setNegativeButton("Cancle",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();

実行様子は添付ファイルにご覧ください。

よろしくお願いいたします。

2010/1/5 Akio Kondo <koj...@gmail.com>:

> --
>
> このメールは Google グループのグループ「日本Androidの会」の登録者に送られています。
> このグループに投稿するには、android-g...@googlegroups.com にメールを送信してください。
> このグループから退会するには、android-group-j...@googlegroups.com にメールを送信してください。
> 詳細については、http://groups.google.com/group/android-group-japan?hl=ja からこのグループにアクセスしてください。
>
>
>

--
CHEN,Zi-zhao
wi...@mrchen.info

2010-01-05_154459.png
2010-01-05_154514.png

mid-_-night

unread,
Jan 7, 2010, 5:28:49 AM1/7/10
to 日本Androidの会
こんどうさん,陳さんありがとうございました.
無事解決することができました.
本当にありがとうございました.
Reply all
Reply to author
Forward
0 new messages