1

ユーザー入力を検証するシステムをセットアップしようとしています。この場合、ユーザーが入力したものがあることを確認するだけです。

にデータがあるUtility classことを確認する がEditTextあります。

OnFocusChangeListenerからメソッドを呼び出すを使用していUtility classます。

これは からのコードですactivity:

editText = (EditText) view.findViewById(R.id.editText);
    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override public void onFocusChange(View v, boolean hasFocus) {

            if (!hasFocus) {
                if (Utility.checkInput(editText) == false) {
                    Toast.makeText(getActivity(), "Enter value!",
                            Toast.LENGTH_SHORT).show();
                    // TODO....
                    // Retain focus - do not allow user to move on.
                    // This is where I am lost...

                }
            }
        }
    });

これは からのコードですUtility class:

public class Utility {
    public static boolean checkInput(EditText editText) {
        if (editText.getText().length() != 0) {
            return true;
        }
        return false;
    }
    //.../
  1. 私が困惑しているのは、チェック入力が false を返した場合にフォーカスを保持する方法です。ユーザーが前に進めないようにしたい。簡単な解決策があると確信していますが、見つけられませんでした。

  2. xml ime.Optionsまた、これが影響するかどうかも疑問です。
    ありがとう。

編集

これは私のxmlです:

<EditText
    android:imeOptions="actionDone"
    android:inputType="textPersonName"
    android:maxLength="24"/>

<spinner ../

<EditText
    android:imeOptions="actionDone"
    android:inputType="phone"
    android:maxLength="12"/>

私が使用する場合:

if (!hasFocus) {
    if (Utility.checkInput(name) == false) {
        Toast.makeText(AddDriverUserActivity.this, "Enter value!",Toast.LENGTH_SHORT).show(); 
         name.requestFocus();
      }
 }

問題は、ユーザーがスピナーから選択して次のEditText. したがって、次の編集テキスト (電話) に触れるまでトーストは表示されず、フォーカスは 2 つの編集テキストに設定されます。

ここに画像の説明を入力

リセットすると:

android:imeOptions="actionDone" スクリーン ショットにactionNext示すように、トーストを表示してから次の編集テキストに進みます。両方ともフォーカスされています。

Request focus編集テキストからの移動を妨げません。

4

2 に答える 2