[英]Android studio close keyboard and finish activity
When I open this activity, it shows the EditText
and the soft keyboard automatically shows up. 当我打开此活动时,它将显示
EditText
并自动显示软键盘。
I want the activity finished when I tap on the back button on the left down corner, not just close the keyboard. 我想在点击左下角的后退按钮时完成活动,而不仅仅是关闭键盘。
You can put this code in you utils to open the keyboard 您可以将此代码放入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();
}
}
Also if you want to hide the keyboard then you can use this code : 另外,如果您想隐藏键盘,则可以使用以下代码:
/**
* 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();
}
}
Use this code in you menifest: 在您的清单中使用以下代码:
android:windowSoftInputMode="adjustPan"
From your question i understand that you want to finish your activity on button click. 根据您的问题,我知道您想在单击按钮时完成活动。 For finish any activity you can use this code
为了完成任何活动,您可以使用此代码
Intent i = new Intent(this,Here is your first activity.Class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
Add this to your activity 将此添加到您的活动中
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
finish();
}
return super.onKeyDown(keyCode, event);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.