ユーザー入力を検証するシステムをセットアップしようとしています。この場合、ユーザーが入力したものがあることを確認するだけです。
にデータがある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;
}
//.../
私が困惑しているのは、チェック入力が false を返した場合にフォーカスを保持する方法です。ユーザーが前に進めないようにしたい。簡単な解決策があると確信していますが、見つけられませんでした。
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
編集テキストからの移動を妨げません。