如果我可以在this topic
询问几乎相同的问题我已在我的activity_main.xml文件中添加:
android:focusable="true"
android:focusableInTouchMode="true"
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context="com.example.stefancvetkovic.stefantest001.MainActivity">
<EditText
android:id="@+id/txtScanedResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp" />
</android.support.constraint.ConstraintLayout>
它工作正常,但当我想在完成事件时隐藏键盘时,键盘保持打开状态。
MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((EditText)findViewById(R.id.txtScanedResult)).setOnEditorActionListener(
new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
event != null &&
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (event == null || !event.isShiftPressed()) {
// the user is done typing.
//HIDE KEYBOARD
EditText edtView=(EditText)findViewById(R.id.txtScanedResult);
edtView.setInputType((InputType.TYPE_NULL));
//
Toast.makeText(getApplicationContext(),"Voila!",Toast.LENGTH_SHORT)
.show();
return true; // consume.
}
}
return false; // pass on to other listeners.
}
});
}
Toast在完成事件时效果很好,但键盘保持打开状态。 Hoiw可以设法在加载时初始化关闭键盘,并隐藏在finishEvent上吗? 我在Android 5.1上运行模拟器
答案 0 :(得分:2)
试试这个
/**
* This function is used to hide soft keyboard
*
* @param context mContext
* @param view view for which keyboard is open
*/
public static void hideSoftInput(Context context, View view) {
if (view != null) {
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (!inputMethodManager.isActive()) {
return;
}
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
/**
* This function is used to hide soft keyboard
*
* @param activity activity
*/
public static void hideSoftInput(Activity activity) {
try {
if (activity != null) {
View focusedView = activity.getCurrentFocus();
hideSoftInput(activity, focusedView);
}
} catch (Throwable t) {
CustomLogHandler.printErrorlog(t);
}
}
/**
* This function is used to show soft keyboard
*
* @param activity activity
*/
public static void showSoftInput(Activity activity) {
try {
if (activity != null) {
View focusedView = activity.getCurrentFocus();
if (focusedView != null) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(focusedView, InputMethodManager.SHOW_IMPLICIT);
}
}
} catch (Throwable t) {
CustomLogHandler.printErrorlog(t);
}
}
/**
* This function is used to show soft keyboard
*
* @param view view for which soft keyboard need to be opened
*/
public static void showSoftInput(final View view) {
try {
if (view == null) {
return;
}
view.requestFocus();
InputMethodManager inputManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(view, 0);
} catch (Exception e) {
CustomLogHandler.printErrorlog(e);
}
}
或尝试
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
答案 1 :(得分:0)
在显示吐司之前调用它。
public void hideKeyboard(Activity context) {
// Check if no view has focus:
View view = context.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
答案 2 :(得分:0)
试试这段代码:
/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
/**
* Shows the soft keyboard
*/
public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}
或者这样做:
在AndroidManifest.xml中:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
或尝试:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);