我想在AlertDialog解雇后隐藏软键盘,但它仍然可见。这是我的代码:
alert = new AlertDialog.Builder(MyActivity.this);
imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
});
答案 0 :(得分:83)
Manifest xml
android:windowSoftInputMode="stateAlwaysHidden"
它会在关闭
时自动隐藏软键盘Dialog
答案 1 :(得分:15)
我遇到了同样的问题。通过这样做解决了它。它不需要任何参考:
imm.hideSoftInputFromWindow(getWindow().getDecorView()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
答案 2 :(得分:6)
关闭警报对话框时遇到类似问题。这似乎对我有用。
在DialogFragment中
public static void closeKB(final View view)
{
caller.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}, 1);
}
@Override
public void onDismiss(DialogInterface dialog)
{
super.onDismiss(dialog);
View view = getActivity().getCurrentFocus();
if (view != null)
{
closeKB(view);
}
}
答案 3 :(得分:5)
我使用这种方法:
IBinder token = searchTextEntry.getWindowToken();
( ( InputMethodManager ) getSystemService( Context.INPUT_METHOD_SERVICE ) ).hideSoftInputFromWindow( token, 0 );
searchTextEntry
是EditText
引用的名称。
答案 4 :(得分:1)
public ActionResult<object> Post([FromBody] A value)
{
return new A { a = 1 };//("Hi", "Hi2");
}
答案 5 :(得分:1)
我在所有这些解决方案上都遇到了麻烦,但是如果您正在使用Fragment,请在以下线程中使用@ amal-dev-s-i:How to hide keyboard on dialog dismiss
我只是将其添加到片段中,并且在对话框上调用dismiss()后可以正常工作:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
如果在父活动中,将上述内容(删除getActivity())添加到父活动的onResume()中,我也有类似的运气。
答案 6 :(得分:0)
所有这些使用InputMethodManager
的建议都有点模糊 - 确切地称之为,它们至少对我来说并不起作用。
是的,键盘消失但随后应用程序崩溃了!?
主要问题是当对话框消失时,隐藏键盘会同时发生。
为了避免dialog.dismiss()
,应在view.postDelayed()
之后imm.hideSoftInputFromWindow()
调用public class DataListActivity extends Activity {
ListView listView;
SQLiteDatabase sqLiteDatabase;
FoodDbHelper foodDbHelper;
Cursor cursor;
ListDataAdapter listDataAdapter;
private Button button1;
ListDataAdapter dataAdapter = null;
Button button;
DataProvider dataProvider;
ArrayList<HashMap<String, String>> namessList;
EditText inputSearch;
String search_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.data_list_layout);
inputSearch = (EditText)findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changes the Text
listDataAdapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
listView = (ListView) findViewById(R.id.list_View);
listDataAdapter = new ListDataAdapter(getApplicationContext(),
R.layout.row_layout) {
@Override
protected void showCheckedButton(int position, boolean value) {
// TODO Auto-generated method stub
DataProvider item = (DataProvider) listDataAdapter
.getItem(position);
Log.i("", "");
item.setSelected(value);
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer responseText = new StringBuffer();
responseText
.append("The following dishes were selected...\n");
ArrayList<DataProvider> list = listDataAdapter
.getSelectedIndexes();
int sum = 0;
for (int i = 0; i < list.size(); i++) {
DataProvider dataProvider = list.get(i);
sum = sum + dataProvider.getCalorie();
responseText.append("\n" + dataProvider.getName()
+ " : " + dataProvider.getCalorie()
+ " kcal"
);
}
Toast.makeText(getApplicationContext(), ""+responseText+"\n"+"................................."
+"\n"+"Total Calories In Your Menu Is : " +sum,
Toast.LENGTH_LONG).show();
}
});
}
};
listView.setAdapter(listDataAdapter);
foodDbHelper = new FoodDbHelper(getApplicationContext());
sqLiteDatabase = foodDbHelper.getReadableDatabase();
cursor = foodDbHelper.getInformations(sqLiteDatabase);
if (cursor.moveToFirst()) {
do {
String name, quantity, fat, protein, sugar, vitamins;
boolean selected = false;
String names = null;
Integer calorie;
name = cursor.getString(0);
quantity = cursor.getString(1);
calorie = Integer.valueOf(cursor.getString(2));
fat = cursor.getString(3);
protein = cursor.getString(4);
sugar = cursor.getString(5);
vitamins = cursor.getString(6);
DataProvider dataProvider = new DataProvider(name, quantity,
calorie, fat, protein, sugar, vitamins, names, selected);
listDataAdapter.add(dataProvider);
} while (cursor.moveToNext());
}
,在我的情况下,我将延迟设置为150。
答案 7 :(得分:0)
这有效!对话框关闭后,这将关闭键盘
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
答案 8 :(得分:0)
万一有人在kotlin中寻找它,那就是:
private fun hideDeviceKeyboard() {
val imm = context!!.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}