问题
我想使用OTP验证更改手机号码。我在用户个人资料屏幕上有一个更改按钮。单击更改按钮时,我打开一个对话框。一旦对话框打开,用户正在键入新的联系人号码并按下重新发送对话框的otp按钮,然后对话框将被关闭,键盘也应该隐藏。但是,键盘并没有隐藏。改变change_no_dialog-box之后的一件事我会再次显示一个用于输入OTP的对话框,一旦用户进入OTP并再次按下提交按钮,同样的问题就会出现键盘没有隐藏。
这里我尝试了
对话框上的Edittext
<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和每个活动布局上添加
android:windowSoftInputMode="stateHidden"
请看ScreenShot
答案 0 :(得分:-1)
试试这个:
public static void hideKeyboard(EditText edt) {
InputMethodManager imm = (InputMethodManager)
edt.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
}
另外在下面添加dismiss listener:
dialogBox.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
hideKeyBoard(editText);
dialogBox.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}
});
答案 1 :(得分:-2)
因为您的焦点视图在对话框上。您需要从视图中删除键盘。 请使用以下代码。
{
"_id" : ObjectId("5a27b609e101b6092b4ebf91"),
"city" : "Mumbai",
"detail" : [
{
"type" : "One",
"name" : "Some name",
"_id" : ObjectId("5a27b609e101b6092b4ebf92"),
"numbers" : [
"72598xxx78",
"81301xxx88",
"83302xxx30",
"84309xxx43",
"85309xxx77",
"86309xxx61",
"87270xxx88",
"85272xxx36",
"88272xxx23",
"85276xxx01"
]
},
{
"name" : "Some name",
"type" : "two",
"_id" : ObjectId("5a28e954d4f5a30527d92a32"),
"contact" : [
"72598xxx78",
"81301xxx88",
"83302xxx30",
"84309xxx43",
"85309xxx77",
"86309xxx61",
"87270xxx88",
"85272xxx36",
"88272xxx23",
"85276xxx01"
]
},
]
}
在解除对话之前调用它。
public static void hideSoftKeyboard(Activity activity, View view) {
if (view != null) {
InputMethodManager inputMethodManager = (InputMethodManager) activity
.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (inputMethodManager.isActive()) {
System.out.println("Keyboard hiden");
inputMethodManager.hideSoftInputFromWindow(view
.getWindowToken(), 0);
} else {
System.out.println("Keyboard not visible");
}
}
}