我试图隐藏我的键盘,因为它失去了对 setOnFocuslistener 的关注。由于某种原因它不起作用......这是我的xml:
<EditText
android:id="@+id/AgePicker"
android:layout_width="113dp"
android:layout_column="2"
android:layout_columnSpan="2"
android:layout_gravity="left|bottom"
android:layout_row="4"
android:background="#ffffff"
android:clickable="true"
android:focusable="true"
android:hint="@string/edit_message"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black_overlay" />
和我的代码:
setAge.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
String sAge = setAge.getText().toString();
Age = Integer.parseInt(sAge);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(setAge.getWindowToken(), 0);
if (Age < 0) {
Toast.makeText(DietActivity.this,
"Enter Computable Values", Toast.LENGTH_SHORT)
.show();
}
}
}
});
我也在我的xml中使用这些:
android:focusable="true"
android:focusableInTouchMode="true"
请帮我。我想要的只是当edittext失去焦点时键盘隐藏