Androidプロンプトユーザ入力ダイアログの例
このチュートリアルでは、前のリンク://android/android-alert-dialog-example/[
AlertDialog
]の例を使用して、
PromptDialog
のようにユーザーの入力を受け入れるようにします。具体的には、これはカスタムのAlertDialogの例です。
次の手順を参照してください。
-
プロンプトダイアログレイアウト(XMLファイル)を作成します.
-
プロンプトのダイアログレイアウトを `AlertDialog.Builder`にアタッチします.
-
AlertDialog.Builder`を
AlertDialog`に添付してください. -
完了しました.
P.SこのプロジェクトはEclipse 3.7で開発され、Android 2.3.3.
でテストされています
1 Androidレイアウトファイル
2つのXMLファイル(メイン画面用とプロンプト用)。
File:res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout__width="fill__parent" android:layout__height="fill__parent" android:orientation="vertical" > <Button android:id="@+id/buttonPrompt" android:layout__width="wrap__content" android:layout__height="wrap__content" android:text="Show Prompt Dialog"/> <EditText android:id="@+id/editTextResult" android:layout__width="match__parent" android:layout__height="wrap__content" > </EditText> </LinearLayout>
File:res/layout/prompts.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout__root" android:layout__width="fill__parent" android:layout__height="fill__parent" android:orientation="vertical" android:padding="10dp" > <TextView android:id="@+id/textView1" android:layout__width="wrap__content" android:layout__height="wrap__content" android:text="Type Your Message : " android:textAppearance="?android:attr/textAppearanceLarge"/> <EditText android:id="@+id/editTextDialogUserInput" android:layout__width="match__parent" android:layout__height="wrap__content" > <requestFocus/> </EditText> </LinearLayout>
2.活動
次のステップでコメントとデモを読んで、それは自己探検でなければなりません。
File:MainActivity.java
package com.mkyong.android; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { final Context context = this; private Button button; private EditText result; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //components from main.xml button = (Button) findViewById(R.id.buttonPrompt); result = (EditText) findViewById(R.id.editTextResult); //add button listener button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //get prompts.xml view LayoutInflater li = LayoutInflater.from(context); View promptsView = li.inflate(R.layout.prompts, null); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( context); //set prompts.xml to alertdialog builder alertDialogBuilder.setView(promptsView); final EditText userInput = (EditText) promptsView .findViewById(R.id.editTextDialogUserInput); //set dialog message alertDialogBuilder .setCancelable(false) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { //get user input and set it to result //edit text result.setText(userInput.getText()); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { dialog.cancel(); } }); //create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); //show it alertDialog.show(); } }); } }
デモ
それを開始すると、 “main.xml”レイアウトはボタンとedittext(結果)を表示します。
ボタンをクリックし、 “prompts.xml”プロンプトダイアログを表示し、 ”
mkyong
“というメッセージを入力し、 “OK”ボタンをクリックします。
ユーザー入力 ”
mkyong
“は、 “main.xml”レイアウト、edittext(result)に渡され、表示されます。
ソースコードをダウンロードする
それをダウンロードする –
Android-Prompt-Dialog-Example.zip
(16 KB)
参考文献
AlertDialog Javadoc]。
http://developer.android.com/guide/topics/ui/dialogs.html
[Android
ダイアログの例]。 link://android/android-custom-dialog-example/[Androidカスタムダイアログ
例]
リンク://タグ/アンドロイド/[アンドロイド]リンク://タグ/ダイアログ/[ダイアログ]
ユーザ入力