我有一个选择框并在视图中编辑文本。如果您对customNumberPicker进行长按,它将隐藏customNumberPicker并显示Edittext。(对编辑文本执行相同的操作)效果很好。
但是我也希望在切换到编辑文本模式时键盘可以打开。
这是我的代码
final NumberPicker numberPicker = (NumberPicker) dialog.findViewById(R.id.npWeight);
final EditText editText = (EditText) dialog.findViewById(R.id.edWeight);
numberPicker.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if (numberPicker.getVisibility() == View.VISIBLE) {
editText.setVisibility(View.VISIBLE);
numberPicker.setVisibility(View.GONE);
editText.setFocusableInTouchMode(true);
editText.requestFocus();
showKeyboard(MyApplication.mainActivity);
}
return true;
}
});
editText.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
editText.setVisibility(View.GONE);
numberPicker.setVisibility(View.VISIBLE);
// hideKeyboard(MyApplication.mainActivity);
return true;
}
});
private static void showKeyboard(Activity activity) {
View view = activity.getCurrentFocus();
InputMethodManager methodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
assert methodManager != null && view != null;
methodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
答案 0 :(得分:1)
尝试显示具有以下选项的键盘:
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(view.getApplicationWindowToken(),InputMethodManager.SHOW_FORCED, 0);
或
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
对于片段,请尝试以下代码:
public void showKeybarod(){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(getActivity() != null && !getActivity().isFinishing() && getUserVisibleHint()) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
}
},300);
}