当我的片段启动,我希望我的EditText处于焦点/让用户只需在它打字。 我能得到它的对焦与requestFocus()方法,但我不能让键盘出现。
我曾经尝试都这样:
edit = (EditText) view.findViewById(R.id.search);
edit.requestFocus();
InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.showSoftInput(edit, 0);
和
edit = (EditText) view.findViewById(R.id.search);
InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.showSoftInput(edit, 0);
edit.requestFocus();
我怎样才能获得键盘露面的EditText?
Answer 1:
工作的呢?
imgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Answer 2:
你可以试试这个
@Override
public void onResume() {
super.onResume();
edit.post(new Runnable() {
@Override
public void run() {
edit.requestFocus();
InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.showSoftInput(edit, InputMethodManager.SHOW_IMPLICIT);
}
});
}
Answer 3:
由于使用showSoftInput
的所有情况,并试图这里提到的一些,像解决方案后不工作:
if (binding.account.requestFocus()) {
getActivity().getWindow()
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
我终于做到了工作中使用 :
if (binding.account.requestFocus()) {
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(
InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY
);
}
以来:
binding.account.requestFocus()
只要求为重点EditText
(不打开键盘)
和
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(
InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY
);
是看起来一切正常,显示键盘的唯一解决方案(以及大多数投一个)
祝好运! :-)
Answer 4:
我对那个有益的扩展:
fun EditText.showKeyboard() {
if (requestFocus()) {
(getActivity()?.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager)
.showSoftInput(this, SHOW_IMPLICIT)
setSelection(text.length)
}
}
你还需要这一个:
fun View.getActivity(): AppCompatActivity?{
var context = this.context
while (context is ContextWrapper) {
if (context is AppCompatActivity) {
return context
}
context = context.baseContext
}
return null
}
Answer 5:
@Override
public void onHiddenChanged (boolean hidden)
{
super.onHiddenChanged(hidden);
if(hidden)
{
hideKeyboard(yourView);
}
else
{
toggleKeyboard(yourView);
}
}
public static void toggleKeyboard(View v)
{
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
v.requestFocus();
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS);
}
public static void hideKeyboard(View v)
{
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
Answer 6:
这里和其他相关的问题,尝试所有的解决方案之后,下面是对我的作品的方法:
editText.postDelayed(Runnable { showKeyboard(activity, editText)} , 50)
fun showKeyboard(activity: Activity, editText: EditText) {
val inputMethodManager = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
editText.requestFocus()
inputMethodManager.showSoftInput(this, 0)
}
有趣的事实是,当你调用它没有postDeleayed它不会工作,即使你只是延缓它的1毫秒它仍然是行不通的:d
您也可以使用它作为像这样的扩展:
fun EditText.showKeyboard(activity: Activity) {
val inputMethodManager = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
requestFocus()
inputMethodManager.showSoftInput(this, 0)
}
如果你不喜欢传递活动作为参数,使用这个扩展功能@Rafols建议:
fun View.getActivity(): AppCompatActivity? {
var context = this.context
while (context is ContextWrapper) {
if (context is AppCompatActivity) {
return context
}
context = context.baseContext
}
return null
}
那么你的方法是这样的:
fun EditText.showKeyboard() {
val inputMethodManager = getActivity()!!.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
requestFocus()
inputMethodManager.showSoftInput(this, 0)
}
此外,如果你在你的EditText有一个文本已在现有文本的末尾其更好集选择:
fun EditText.showKeyboard() {
val inputMethodManager = getActivity()!!.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
requestFocus()
inputMethodManager.showSoftInput(this, 0)
setSelection(length())
}
如果你想启动它,当片段启动:
override fun onResume() {
super.onResume()
editText.postDelayed(Runnable { editText.showKeyboard()} , 50)
}
Answer 7:
使键盘打开您的片段开始时的另一种方法是调用requestFocus()
中onCreateView
和打开,当且仅当键盘做出相应反应EditText
可获得焦点。
if(this.editText.requestFocus())
{
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
Answer 8:
作为Nilzor说,这个工程
imgr.showSoftInput(getView(), InputMethodManager.SHOW_IMPLICIT)
我同意它比toogleSoftInput一个最佳的解决方案
Answer 9:
这篇文章帮助我
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_edit_name, container);
editText = (EditText) view.findViewById(R.id.txt_yourName);
editText.requestFocus();
getDialog().getWindow().setSoftInputMode(
LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return view;
}
https://turbomanage.wordpress.com/2012/05/02/show-soft-keyboard-automatically-when-edittext-receives-focus/
Answer 10:
简单地说,使用添加2号线将工作就像一个魅力:
如果使用XML
android:focusable="true"
android:focusableInTouchMode="true"
否则,在Java中:
view.setFocusableInTouchMode(true);
view.requestFocus();
文章来源: Show keyboard for edittext when fragment starts