如何以编程方式在EditText上设置焦点(并显示键盘)

Hou*_*ine 159 android focus android-edittext

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

<LinearLayout>
<TextView...>
<TextView...>
<ImageView ...>
<EditText...>
<Button...>
</linearLayout>
Run Code Online (Sandbox Code Playgroud)

如何以EditText编程方式设置焦点(显示键盘)?

我已经尝试了这个,只有当我Activity正常启动时它才有效,但是当我在a中启动它时TabHost,它不起作用.

txtSearch.setFocusableInTouchMode(true);
txtSearch.setFocusable(true);
txtSearch.requestFocus();
Run Code Online (Sandbox Code Playgroud)

Dav*_*man 315

试试这个:

EditText editText = (EditText) findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
Run Code Online (Sandbox Code Playgroud)

http://developer.android.com/reference/android/view/View.html#requestFocus()

  • 这不起作用.这一次对我的作品InputMethodManager IMM =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); (24认同)
  • 它只在我正常启动我的活动时有效,但当我在TabHost上启动我的活动时,它不起作用, (5认同)
  • “这不行,兄弟”。在某些情况下,您需要从postDelayed()异步调用此代码。我遇到了一种情况,当用户在对话框上按“确定”后必须打开键盘。当对话框关闭时,焦点变得混乱了。因此,我已经从postDelayed()调用了上面的代码。在对话框关闭后执行。利润。 (4认同)
  • 对答案的 237 票和对“它不起作用兄弟”的 62 票我对其进行了测试以获得自己的意见,并且效果很好!) (2认同)
  • 只是为了分享经验:我刚刚将代码添加到当前应用程序项目中的四个不同片段中。对于前三个片段,代码可以完美运行。对于最后一个片段,没有显示键盘,直到我使用 Kotlin 协程从 onViewCreated 启动代码(延迟 100 毫秒)。 (2认同)
  • 附录:我发现另一个片段,在执行代码之前我需要 100 毫秒的延迟加上 view.clearFocus() 。 (2认同)

ung*_*rys 159

使用:

editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Run Code Online (Sandbox Code Playgroud)

  • 在尝试了超过5种其他方法之后,这是唯一一个适合我的方法(来自`View`子类) (20认同)
  • 这个建议导致键盘被固定,即使场失去焦点. (12认同)
  • 虽然这种方法确实有效,但它有一个缺点,退出应用程序与主页按钮(硬件)将让键盘在屏幕上.您必须按下返回按钮(硬件)以隐藏键盘,鄙视它在主屏幕上无用. (7认同)
  • 是的,它也适用于我,而`imm.showSoftInput()`不起作用. (2认同)

Dan*_*spa 49

这对我有用,感谢ungalcrys

显示键盘:

editText = (EditText)findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(this.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
Run Code Online (Sandbox Code Playgroud)

隐藏键盘:

InputMethodManager imm = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)

  • 唯一的完整解决方案.谢谢. (2认同)

vin*_*odi 40

showSoftInput 根本不适合我.

我想我需要设置输入模式:( android:windowSoftInputMode="stateVisible"此处在清单中的Activity组件中)

希望这有帮助!

  • 这只是在活动开始时显示键盘. (5认同)

小智 34

final EditText tb = new EditText(this);
tb.requestFocus();
tb.postDelayed(new Runnable() {
    @Override
    public void run() {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(tb, InputMethodManager.SHOW_IMPLICIT);
    }
}, 1000);
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!这甚至适用于 0 毫秒 (`tb.post({ showKeyboard(tb) })`)。请注意,我们需要一个 EditText 视图(`tb`),而不是片段视图。 (4认同)
  • 就在那里。这就是我一直在寻找的答案。不过,您不一定需要整整一秒的延迟。我只尝试了 150 毫厘,效果也很好。 (3认同)

alv*_*lof 12

以下是如何使用kotlin扩展来显示和隐藏软键盘:

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

fun View.hideKeyboard() {
  val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
  inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
Run Code Online (Sandbox Code Playgroud)

然后你可以这样做:

editText.showKeyboard()
// OR
editText.hideKeyboard()
Run Code Online (Sandbox Code Playgroud)

  • 与休息相比,这是一个更好的解决方案 (3认同)
  • 这在 2021 年 3 月对我有用。是在 Handler(Looper.getMainLooper()).postDelayed( { ... }, 1000) 中实现的 (2认同)

Ham*_*han 6

这是用于隐藏和显示键盘的 KeyboardHelper 类

import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * Created by khanhamza on 06-Mar-17.
 */

public class KeyboardHelper {
public static void hideSoftKeyboard(final Context context, final View view) {
    if (context == null) {
        return;
    }
    view.requestFocus();
    view.postDelayed(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}, 1000);
}

public static void hideSoftKeyboard(final Context context, final EditText editText) {
    editText.requestFocus();
    editText.postDelayed(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
}, 1000);
}


public static void openSoftKeyboard(final Context context, final EditText editText) {
    editText.requestFocus();
    editText.postDelayed(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
}, 1000);
}
}
Run Code Online (Sandbox Code Playgroud)


Spi*_*pau 6

我建议使用LifecycleObserver这是一部分具有生命周期感知组件处理生命周期安卓Jetpack的

我想在片段/活动出现时打开和关闭键盘。首先,为 EditText定义两个扩展函数。您可以将它们放在项目中的任何位置:

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

fun EditText.hideKeyboard() {
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(this.windowToken, 0)
}
Run Code Online (Sandbox Code Playgroud)

然后定义一个 LifecycleObserver,它在 Activity/Fragment 到达onResume()或时打开和关闭键盘onPause

class EditTextKeyboardLifecycleObserver(private val editText: WeakReference<EditText>) :
    LifecycleObserver {

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun openKeyboard() {
        editText.get()?.postDelayed({ editText.get()?.showKeyboard() }, 100)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun closeKeyboard() {
        editText.get()?.hideKeyboard()
    }
}
Run Code Online (Sandbox Code Playgroud)

然后将以下行添加到您的任何片段/活动中,您可以随时重用 LifecycleObserver。例如对于片段:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

    // inflate the Fragment layout

    lifecycle.addObserver(EditTextKeyboardLifecycleObserver(WeakReference(myEditText)))

    // do other stuff and return the view

}
Run Code Online (Sandbox Code Playgroud)


小智 6

将其放入 onResume() 方法中。

binding.etxtSearch.isFocusableInTouchMode = true
binding.etxtSearch.isFocusable = true
binding.etxtSearch.requestFocus() 
val inputMethodManager = context?.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(binding.etxtSearch, InputMethodManager.SHOW_IMPLICIT)
Run Code Online (Sandbox Code Playgroud)