如果要清除对编辑文本的默认焦点,请使用以下 2 个属性
android:focusable="false"
android:focusableInTouchMode="true"
在父线性布局内。
例如:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="true"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="email id"
android:inputType="textEmailAddress"
android:maxLines="1"
/>
</LinearLayout>
如果您想在创建活动时隐藏键盘,请使用清单文件活动标签中的这些属性,例如:
<activity
android:configChanges="screenSize|orientation|keyboardHidden"
android:screenOrientation="portrait"
android:name=".activities.LoginActivity"
android:windowSoftInputMode="stateHidden|adjustResize"/>
如果要在单击按钮或发生某些事件时隐藏键盘,请使用以下代码
public void onClick(View v) {
try {
//InputMethodManager is used to hide the virtual keyboard from the user after finishing the user input
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText()) {
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
} catch (NullPointerException e) {
Log.e("Exception", e.getMessage() + ">>");
}
}
} catch (NullPointerException e) {
Log.e("Exception", e.getMessage() + ">>");
}
如果您想在离开活动后从编辑文本字段中取消焦点
@Override
protected void onResume() {
super.onResume();
mEmail.clearFocus();
mPassword.clearFocus();
}
最后,如果您想在提交表单时清除编辑文本字段中的数据,请使用
@Override
protected void onResume() {
super.onResume();
mEmail.getText().clear();
mPassword.getText().clear();
}