android:编程调用软键盘的问题

标签 android android-input-method

在我的应用程序中,我想显示一个内容列表,并为用户提供使用软键盘过滤列表的能力。为此,我添加了一个应该触发(隐藏/显示)用于过滤的软键盘的按钮。我不想有一个可见的编辑文本控件,因为它会占用不必要的空间。而不是那样,我想在用户键入时显示一个 toast,显示过滤器查询,就像 ListView 的“android:textFilterEnabled”属性一样。据我所知,没有明显的方法可以使用可用的 Android 组件执行此操作。所以我尝试了以下方法:

1) 创建包含不可见编辑文本和 ListView 的布局:

<ListView android:id="@+id/main_list"
          android:drawSelectorOnTop="false"
          android:layout_height="0px"
          android:layout_width="fill_parent"
          android:layout_weight="5"
          />

2) 添加按钮作为弹出窗口并在单击时调用 InputMethodManager 以切换软输入(在 onCreate 中调用):

private void initButton() {
    Button buttonView = (Button) getLayoutInflater().inflate(R.layout.keyboard_button, null);
    buttonView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            final View target = findViewById(R.id.filterbox);
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            // this does not work...
            // imm.toggleSoftInputFromWindow(target.getWindowToken(), InputMethodManager.SHOW_FORCED, 0);
            // ... so need to track this in an instance variable - which sucks
            if (imeShowing) {
                imm.hideSoftInputFromWindow(target.getWindowToken(), 0);
                imeShowing = false;
            } else {
                // check that the filterbox got focus
                Preconditions.checkState(target.requestFocusFromTouch());
                Preconditions.checkState(target.hasWindowFocus());
                Preconditions.checkState(target.hasFocus());
                imm.showSoftInput(target, 0);
                imeShowing = true;
            }
        }
    });
    buttonPopup = new PopupWindow(buttonView);
    // ... code to display the button as a small popup
}

如代码示例中所述,“显而易见”的方法(调用 toggleSoftInput)不起作用,因此我不得不恢复到这种丑陋的 if-else。然而,这是次要问题。主要问题是,当我在模拟器中运行它时,软键盘显示正确,但一旦我开始输入,系统就会开始对谷歌搜索 Activity 的 Intent !并且输入的字符会出现在作为结果显示的 Google 搜索框中。更奇怪的是,这只会在我部署和运行应用程序后第一次输入时发生。 IE。如果我从 Google 搜索框返回到我的应用程序,一切都会按预期进行(不会重定向到搜索框)。在显示之前,我确保不可见的编辑文本获得焦点,所以它应该是软键盘的目标,对吧??

有人知道这里发生了什么吗?

最佳答案

对不起,这是我(这个问题的作者),由于新的“很棒”的 OpenID 登录,我无法访问我的旧帐户:/

所以我发现我真正需要做的只是打开列表的“android:textFilterEnabled”属性,并在单击按钮时关注它。 ListView 支持来自软键盘的输入(尽管我是通过查看 AbsListView 的实际代码而不是在任何文档中获得提示来弄清楚的:)。此外,我设法让 PopupWindow 为按钮工作(而不是我之前尝试过的 Dialog),因此焦点问题消失了。下面的工作代码(以防有人遇到类似问题):

View popupView = getLayoutInflater().inflate(R.layout.keyboard_button, null);
Button buttonView = (Button) popupView.findViewById(R.id.keyboardButton);
buttonView.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
         InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
         final View list = findViewById(R.id.the_list);
         list.requestFocus();
         imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
     }
});
buttonPopup = new PopupWindow(this);

列表将像处理硬键盘一样处理来自软键盘的输入。诀窍就是触发键盘并专注于列表。您还可以通过在其关联的适配器上设置自定义 QueryFilterProvider 来控制如何将过滤应用于列表。

关于android:编程调用软键盘的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4920989/

相关文章:

android - 如何强制更新配置 Activity ?

android - 软键盘不会在 android 中以编程方式隐藏

android - 更改android键盘中的字体样式

android - 使用自定义输入法的输入字段

java - 强制切换输入法

java - 将数据从数据库检索到我的应用程序(不工作)

java - 直接生成二维码到ImageView?

android - 三星的 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS

android - 在滚动时显示和隐藏 BottomAppBar

Android - 判断某个InputMethod(自定义键盘)的调用应用