3

我有一个在操作栏中实现自动完成文本视图的应用程序。我正在使用 Action Bar Sherlock,此自动完成功能由标签显示

android:actionLayout="@layout/field_search"

xml 菜单中的项目,如下所示。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/autocomplete_menu_item"
        android:showAsAction="always|collapseActionView"
        android:actionLayout="@layout/field_search"
        android:icon="@drawable/ic_search"
    />
</menu>

单击搜索图标时,将为用户显示键盘。当用户选择和项目时,自动完成文本视图内容被修改为所选项目名称,并且键盘被隐藏。嗯,这部分工作正常。

不起作用的是键盘仅在自动完成点击事件时被隐藏。因此,如果我单击搜索项而不选择自动完成文本视图的项,然后单击操作栏的主页按钮(关闭当前活动),则键盘不会关闭。它保持打开状态。

我正在通过以下方法设置主页按钮(操作栏的)单击:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // Action bar back button.
        case android.R.id.home:
            onBackPressed();
            return true;
        case R.id.autocomplete_menu_item:
            initializeAutoComplete();
            return true;
        // Default.
        default:
            return super.onOptionsItemSelected(item);
    }
}

因此,调用了 onBackPressed() 方法。但是键盘没有关闭。

然后,我尝试在 onPause() 方法处关闭键盘,如下图:

@Override
protected void onPause() {
    super.onPause();

    // Closes keyboard before exit.
    if (mKeyboardShown)
        hideKeyboard(mAutoComplete);
}

再一次,键盘没有被关闭。

打开和关闭键盘的方法如下所示:

/**
 * Shows the keyboard.
 * 
 * @param view
 */
public void showKeyboard(View view) {
    Context context = view.getContext();
    InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}

/**
 * Hides the keyboard.
 * 
 * @param view
 */
public void hideKeyboard(View view) {
    Context context = view.getContext();
    InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

管理自动完成的方法如下所示。

/**
 * Calls the auto complete text view.
 */
public boolean initializeAutoComplete() {
    if (mAutoComplete == null) return false;

    // Cleans text.
    mAutoComplete.setText("");

    // Invoke virtual keyboard.
    mAutoComplete.requestFocus();
    showKeyboard(mAutoComplete);
    mKeyboardShown = true;

    // Creates an array adapter to display the school units from the auto complete text view.
    final AutoCompleteAdapter adapter = new AutoCompleteAdapter(this, mList);

    // Sets the click listener of the auto complete text view, to show the keyboard when the auto complete
    // text view has shown the keyboard and this keyboard was closed (so the auto complete text view is been
    // shown, but the keyboard don't, so we need to show it).
    mAutoComplete.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showKeyboard(mAutoComplete);
            mKeyboardShown = true;
        }
    });

    // Sets the click listener of the auto complete text view, to set the adapter. 
    mAutoComplete.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // Populate list with our static array of titles.
            mAutoComplete.setAdapter(adapter);

            return false;
        }
    });

    // Sets the item click listener of the auto complete text view, to set the auto complete text view name,
    // hide the keyboard, and hide the auto complete drop down.
    mAutoComplete.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Get id of the unit from the adapter tag.
            Integer nameId = (Integer)view.getTag();

            // Get clicked id of the auto complete text view.
            Data data = getDataById(nameId);
            if (data != null) mAutoComplete.setText(data.getName());
            else mAutoComplete.setText("Data not found.");

            // Hide keyboard and hide auto complete drop down.
            hideKeyboard(mAutoComplete);
            mKeyboardShown = false;
            mAutoComplete.setDropDownHeight(0);
        }
    });

    return true;
}

因此,每次我调用 showKeyboard() 时都会显示键盘,但只有在自动完成文本视图的单击事件中调用 hideKeyboard() 时才会隐藏。

似乎 hideKeyboard() 仅在调用单击事件时获取“窗口令牌”。但是我需要在需要时关闭键盘,而不仅仅是在单击事件时。

在这个应用程序中有两个活动:MainActivity 和 AutoCompleteActivity。下面我展示了 1 张图片,分为 6 帧(从左到右/从上到下),显示了以下操作:

图片链接

  1. 之后进入AutoCompleteActivity。
  2. 单击搜索图标后(此处称为键盘)。
  3. 输入一些文本后,自动完成显示适配器找到的内容。
  4. 单击自动完成显示的项目后,自动完成文本内容将更新为所选项目的名称(在这种情况下,“Apple”为白色)。
  5. 单击操作栏后退按钮后,自动完成文本视图被隐藏,但不是键盘,甚至调用 onBackPressed() (当调用 onOptionsItemSelected() 时)。
  6. 再次按下操作栏的返回按钮,我们回到了 MainActivity。但即使调用了 AutoCompleteActivity 的 onPaused(),键盘仍保留在屏幕上。这怎么可能发生?

我如何解决这个问题?

4

1 回答 1

0

所以当我按下操作栏的后退按钮时关闭键盘

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    System.out.println("itemid: " + item.getItemId());
    switch (item.getItemId()) {
    case MENU_SEARCH:

        item.setActionView(R.layout.searchedittext);
        filterEdit = (EditText) item.getActionView().findViewById(
                R.id.filtrar2);

        //Listener que se encarga de registrar los eventos de cuando se expande y se contrae la barra de busqueda en la actionbar
        item.setOnActionExpandListener(new OnActionExpandListener() {

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                // TODO Auto-generated method stub
                return true;
            }

            //Cerramos el teclado al contraer la barra de busqueda
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                // TODO Auto-generated method stub
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(
                        filterEdit.getWindowToken(), 0);

                return true;
            }
        });

        break;
    case android.R.id.home:

        break;

    default:
        break;
    }
    return super.onOptionsItemSelected(item);
}
于 2012-08-06T12:08:27.900 回答