当我打开此活动时,它会显示EditText
,并自动显示软键盘。
我希望在点击左下角的后退按钮时完成活动,而不仅仅是关闭键盘。
答案 0 :(得分:0)
您可以将此代码放入utils中以打开键盘
/**
* Method to show keyboard
*
* @param context Context of the calling activity
*/
public static void showKeyboard(Context context) {
try {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(((Activity) context).getCurrentFocus(), InputMethodManager.SHOW_IMPLICIT);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Method to show keyboard
*
* @param context Context of the calling activity
* @param editText Edittext which will receive focus
*/
public static void showKeyboard(Context context, EditText editText) {
showKeyboard(context);
try {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
imm.showSoftInput(((Activity) context).getCurrentFocus(), InputMethodManager.SHOW_FORCED);
} catch (Exception e) {
e.printStackTrace();
}
}
如果您想隐藏键盘,也可以使用以下代码:
/**
* Method to hide keyboard
*
* @param mContext Context of the calling class
*/
public static void hideKeyboard(Context mContext) {
try {
InputMethodManager inputManager = (InputMethodManager) mContext
.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(((Activity) mContext).getCurrentFocus().getWindowToken(), 0);
} catch (Exception ignored) {
ignored.printStackTrace();
}
}
在您的清单中使用此代码:
android:windowSoftInputMode="adjustPan"
答案 1 :(得分:0)
从您的问题中我了解到您想在单击按钮时完成活动。为了完成任何活动,您可以使用此代码
Intent i = new Intent(this,Here is your first activity.Class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
答案 2 :(得分:-1)
将此添加到您的活动中
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
finish();
}
return super.onKeyDown(keyCode, event);
}