Androidプロンプトユーザ入力ダイアログの例

Androidプロンプトのユーザー入力ダイアログの例

このチュートリアルでは、前のAlertDialogの例を拡張して、PromptDialogと同じようにユーザー入力を受け入れることができるようにします。 より具体的には、これはカスタムAlertDialogの例です。

次の手順を参照してください。

  1. プロンプトダイアログレイアウト(XMLファイル)を作成します。

  2. プロンプトダイアログレイアウトをAlertDialog.Builderに添付します。

  3. AlertDialog.BuilderAlertDialogにアタッチします。

  4. 完了しました。

P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.

Note
このcustom dialog exampleを読むことをお勧めします。

1 Androidレイアウトファイル

メイン画面用とプロンプトダイアログ用の2つのXMLファイル。

ファイル:res / layout / main.xml



    

ファイル:res / layout / prompts.xml



    

    

        

    

2. アクティビティ

次のステップでコメントとデモを読んでください。それは自己探求的です。

ファイル:MainActivity.java

package com.example.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();

            }
        });
    }
}

3. Demo

それを開始すると、「main.xml」レイアウトはボタンと編集テキスト(結果)を表示します。

android prompt user input example

ボタンをクリックし、プロンプトダイアログ「prompts.xml」レイアウトを表示し、メッセージ「example」を入力して、「OK」ボタンをクリックします。

android prompt user input example

ユーザー入力「example」は「main.xml」レイアウトに渡され、テキスト(結果)を編集して表示します。

android prompt user input example

ソースコードをダウンロード

ダウンロード–Android-Prompt-Dialog-Example.zip(16 KB)