android - 按需显示软键盘

发布于 2024-11-17 14:19:42 字数 557 浏览 6 评论 0原文

您好,我将 edittext 控件包装到根据用户请求显示在屏幕上的控件上。它覆盖整个屏幕,直到用户按下键盘上的“完成”按钮。

我无法在屏幕上明确显示控件。仅当用户点击控件时才会显示。我错过了什么吗?

我什至尝试了这个,当我启动存在编辑文本的覆盖层时,它并没有出现:

customCOntrol.showKeyboard();

public void showKeyboard()
    {
        InputMethodManager imm = (InputMethodManager)_context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(this._textView.getWindowToken(), InputMethodManager.SHOW_IMPLICIT);
    }

这是我在配置文件 android:windowSoftInputMode="stateHidden|adjustPan" 中屏幕本身上的设置

提前谢谢

Hi I wrapped edittext control onto a control that is being displayed on the screen at users request. It overlays the whole screen until user presses 'done' button on the keyboard.

I am not able to explicitly show the control on the screen. only when user taps into control only then its shown. Am I missing something?

I even try this and it does not brin it up when I launch the overlay that Edit Text exists on:

customCOntrol.showKeyboard();

public void showKeyboard()
    {
        InputMethodManager imm = (InputMethodManager)_context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(this._textView.getWindowToken(), InputMethodManager.SHOW_IMPLICIT);
    }

here is the settig I have on the screen itself in the config file android:windowSoftInputMode="stateHidden|adjustPan"

Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡水深流 2024-11-24 14:19:42

在您正在调用的 showKeyboard 函数中:

 imm.hideSoftInputFromWindow(this._textView.getWindowToken(), InputMethodManager.SHOW_IMPLICIT);

这将从窗口中隐藏 softInput 键盘!
您想显示键盘吗?如果是,那么您会使用:

 imm.showSoftInput(view, flags, resultReceiver);

编辑:我认为您也可以从 InputMethodManager 切换键盘,尝试:

 imm.toggleSoftInput(0, 0);

In your showKeyboard function you are calling:

 imm.hideSoftInputFromWindow(this._textView.getWindowToken(), InputMethodManager.SHOW_IMPLICIT);

This will hide the softInput keyboard from the window!
Do you want to show the keyboard? If yes then would you use:

 imm.showSoftInput(view, flags, resultReceiver);

EDIT: I think you can also toggle the keyboard from the InputMethodManager, try:

 imm.toggleSoftInput(0, 0);
救赎№ 2024-11-24 14:19:42

@dropsOfJupiter

您可以在启动包含 EditText 引用的 Activity 或 Fragment 时执行以下操作: editText.requestFocus() 。这会将焦点移至 EditText 并带出软键盘。

我希望这有帮助。

@dropsOfJupiter

You can do: editText.requestFocus() as you launch the Activity or Fragment containing your EditText reference. This will give the focus to the EditText and will bring uo the SoftKeyboard.

I hope this helps.

背叛残局 2024-11-24 14:19:42

问题:

我遇到了键盘不显示的问题。我受此答案启发编写了以下解决方案,但不是他们的解决方案!效果很好。简而言之,造成这种混乱的原因是请求焦点和 IMM 提供的服务只能在已创建且活动的视图上运行。当您在创建阶段 onCreate(Bundle savingInstance).. 或 onCreateView(LayoutInflater inflater... 执行所有这些操作并且视图仍处于初始化状态时,您将不会获得活动视图来操作我见过许多使用延迟和检查来等待该视图激活然后显示键盘的解决方案,但这是我基于 android 框架工作设计的解决方案:

解决方案:

在您的活动中或片段覆盖以下内容确保您的视图具有访问权限(在活动/片段的顶部定义它):

@Override
public void onStart() {
    yourView.requestFocus();
    showSoftKeyboard(yourView);
    super.onStart();
}
public void showSoftKeyboard(View view) {
    if(view.requestFocus()){
        InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view,InputMethodManager.SHOW_IMPLICIT);
    }
}

PROBLEM:

I faced with this keyboard not showing up problem. I wrote the following solution inspired by this answer but not their solution! It works fine. In short the reason for this mess is that the request focus and the IMM provided service can only run on a view that is created and active. When you do all these on the creation phase onCreate(Bundle savedInstance).. or onCreateView(LayoutInflater inflater... and the view is still in initializing state, you won't get an active view to act on! I have seen many solutions using delays and checks to wait for that view to get active then do the show keyboard but here is my solution based on the android frame work design:

SOLUTION:

in your activity or fragment override the following make sure your view has the access (define it in the top of the activity/fragment):

@Override
public void onStart() {
    yourView.requestFocus();
    showSoftKeyboard(yourView);
    super.onStart();
}
public void showSoftKeyboard(View view) {
    if(view.requestFocus()){
        InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view,InputMethodManager.SHOW_IMPLICIT);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文