入力ダイアログ表示をループさせる

テキスト入力のあるダイアログを表示して任意に判定した結果、不正な入力なら再表示するように、
ループを実行する方法は、android.os.Handler を利用すると簡単に書ける。

以下、メールアドレス入力させるサンプルで、正しく入力しないと、キャンセルしない限り、
入力ダイアログを表示し続けるようにする例です。

入力ダイアログで入力した内容を Handler メッセージで受信して判定して
再度、入力ダイアロを表示するようにする。

final Handler handler = new Handler(){
   @Override
   public void handleMessage(Message msg){

      String inputText = msg.obj.toString();
      removeMessages(0);
      // varidation(String) → private メソッドで入力情況をチェック
      if (varidation(inputText)){
         // 正常入力された → inputText文字列を処理
         // 入力ダイアログ表示しない。


      }else{
         // 入力ダイアログ表示する。
         LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View layout = inflater.inflate(R.layout.inputdialog,null);
         TextView dirpath = (TextView)layout.findViewById(R.id.inputguideTextView);
         dirpath.setText("メールアドレスを入力してください");
         final EditText editText = (EditText)layout.findViewById(R.id.editTextDialog);
         editText.setText(inputText);
         new AlertDialog.Builder(SampleActivity.this)
         .setView(layout)
         .setCancelable(false)
         .setPositiveButton("OK",new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog,int w){
               Message message = Message.obtain();
               message.obj = editText.getText().toString();
               sendMessage(message);

            }
         })
         .setNegativeButton("キャンセル",new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog,int w){
            }
         }).create().show();
      }
   }

   Pattern pattern 
= Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*((\\.[A-Za-z]{2,}){1})$"
, Pattern.CASE_INSENSITIVE );

   private boolean varidation(String text){
      if (text.length()==0) return false;
      return pattern.matcher(text).matches();
   }

};

// 画面上のあるボタンを押したら入力ダイアログを表示を開始する。

Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener(){
   @Override
   public void onClick(View v){
      Message message = Message.obtain();
      message.obj = "";

      handler.sendMessage(message);
   }
});

ダイアログのレイアウト、R.layout.inputdialog の XMLは、、

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/inputguideTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:text="TextView" />
    <EditText
        android:id="@+id/editTextDialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:inputType="text" >
        <requestFocus />
    </EditText>
</LinearLayout>


ダイアログ表示、、、

 

f:id:posturan:20160313225106p:plain