【发布时间】:2017-12-07 09:20:14
【问题描述】:
问题
我想使用 OTP 验证更改手机号码。我在用户个人资料屏幕上有一个更改按钮。单击更改按钮时,我正在打开一个对话框。一旦对话框打开,用户正在输入他的新联系号码并按下对话框的重新发送 otp 按钮,然后对话框将关闭并且键盘也应该隐藏。但是,键盘并没有隐藏。 change_no_dialog-box 关闭后的一件事,一旦用户输入 OTP 并再次按下提交按钮,我将再显示一个输入 OTP 对话框,同样的问题是键盘没有隐藏。
这是我尝试过的
编辑对话框上的文本
<android.support.design.widget.TextInputLayout
android:layout_marginTop="36dp"
android:theme="@style/TextLabel"
android:id="@+id/no_layout"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:paddingLeft="8dp"
android:id="@+id/dialog_box_mobile"
android:layout_width="match_parent"
android:layout_height="60dp"
android:inputType="number"
android:imeOptions="actionDone"
android:singleLine="true"
android:textColorHint="@color/navigation_header"
android:hint="@string/mobno"
android:textSize="@dimen/sisteen"
android:maxLength="10"/>
</android.support.design.widget.TextInputLayout>
点击重新发送 otp 按钮
if (v.getId() == R.id.resend_otp_btn) {
if (dialog_box_mobile.getText().toString().equals(user_mobile_no)) {
hideKeyboard(EditProfileActivity.this);
dialog.dismiss();
bottomSnackbarMessage("It looks like same number !");
// Toast.makeText(EditProfileActivity.this, "It looks like same number!", Toast.LENGTH_SHORT).show();
} else {
if (dialog_box_mobile.getText().length() != 0) {
if (dialog_box_mobile.getText().toString().length() < 10) {
dialog_box_mobile.setError("Invalid phone number !");
resend_otp_btn.setClickable(true);
} else {
if (CheckNetwork.isConnected()) {
resend_otp_btn.setClickable(false);
dialog.dismiss();
startPhoneNumberVerification(dialog_box_mobile.getText().toString());
} else {
dialog.dismiss();
bottomSnackbar();
}
}
} else if (dialog_box_mobile.getText().toString().equalsIgnoreCase("")) {
dialog_box_mobile.setError("Mobile number required");
resend_otp_btn.setClickable(true);
}
}
}
键盘隐藏方法
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager)
activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window
token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can
grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
在 mainifest 和每个 Activity 布局上添加这个
android:windowSoftInputMode="stateHidden"
请看截图
【问题讨论】:
-
android:windowSoftInputMode="stateAlwaysHidden"