如何在程序中编程设置EditText的焦点(并显示键盘)

235

我有一个布局,其中包含一些像这样的视图:

<LinearLayout>
<TextView...>
<TextView...>
<ImageView ...>
<EditText...>
<Button...>
</linearLayout>

如何在程序中设置焦点(显示键盘)到我的EditText上?

我尝试了这个方法,当我正常启动我的Activity时它有效,但当我在TabHost中启动它时不起作用。

txtSearch.setFocusableInTouchMode(true);
txtSearch.setFocusable(true);
txtSearch.requestFocus();

http://www.android-ios-tutorials.com/93/android-show-hide-soft-keyboard-programmatically/ - Houcine
可能是重复的问题:如何在EditText获得焦点时显示软键盘 - raukodraug
21个回答

2
editTxt.setOnFocusChangeListener { v, hasFocus ->
            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            if (hasFocus) {
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
            } else {
                imm.hideSoftInputFromWindow(v.windowToken, 0)
            }
        }

2

我知道这是一个晚回复,但对于像我这样在2022年想要做这件事情的人来说,发现toggleSoftInput已经被弃用(自level 31起),这里是使用showSoftInput的新方法:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
editView.requestFocus();
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                        .showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);

我尝试了toggleSoftInput,但发现一些问题,比如当我按下home键时键盘仍然存在,但这种方法对我完美地起作用。


2
我正在使用这段代码来编程显示键盘。
binding!!.etAssignToName.postDelayed( {
                mActivity.runOnUiThread {
                    showKeyboard(binding!!.etAssignToName,mContext)
                }
            },300)

fun showKeyboard(editText: EditText, context: Context) {
editText.requestFocus()
val imm: InputMethodManager =
    context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(editText, 0)}

fun View.hideSoftKeyboard(context: Context) {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(this.windowToken, 0)}

2

我尝试了David Merriman的顶级答案,但在我的情况下也没有起作用。但我发现建议延迟运行此代码(这里),它像魔法一样工作。

val editText = view.findViewById<View>(R.id.settings_input_text)

editText.postDelayed({
    editText.requestFocus()

    val imm = context.getSystemService(INPUT_METHOD_SERVICE) as? InputMethodManager
    imm?.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
}, 100)

2

和其他人一样,我不得不在设置焦点后添加延迟才能显示键盘。但是,你的情况可能有所不同。

private fun View.showKeyboard() {
    this.requestFocus()
    this.postDelayed({
        val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
    }, 100)
}

1

我终于想出了一个解决方法,并为此创建了一个 Kotlin 类

object KeyboardUtils {

    fun showKeyboard(editText: EditText) {
        editText.requestFocus()
        val imm = editText.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(editText, 0)
    }

    fun hideKeyboard(editText: EditText) {
        val imm = editText.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(editText.windowToken, 0)
    }

}

1
第一种方式:
    etPassword.post(() -> {
        etPassword.requestFocus();
        InputMethodManager manager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.showSoftInput(etPassword, InputMethodManager.SHOW_IMPLICIT);
    });

第二种方法:
在清单文件中:
    <activity
        android:name=".activities.LoginActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateVisible"/>

在代码中:

etPassword.requestFocus();

1

Kotlin:

fun View.showKeyboard() {
  this.requestFocus()
  val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
  imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}

fun View.hideKeyboard() {
  val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
  imm.hideSoftInputFromWindow(windowToken, 0)
}

使用:

yourEditText.post{
    yourEditText.showKeyboard()
}

0
在"OnCreate"中将光标移动到输入字段,但键盘不会打开:
window().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
)
editText.requestFocus()

之后,当用户输入文本时:

val imm = editText.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
window.setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
)
imm.showSoftInput(editText, 0)

将会正常工作,并在需要时打开键盘,无需设置时间延迟。 记住,在输入文本后,您必须:

window.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
)
requestFocus()

0

我只用了一行代码就完成了它

firstInputField.setNextFocusDownId(R.id.id_of_next_targeted_text_field);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接