19

私は Jake Wharton の優れたActionBarSherlockライブラリを使用しており、折りたたみ可能な検索アクション ビューを持っています。検索アクション ビューが展開されているときに、ソフト キーボードをポップアップ表示したい。

Google の「 Using DialogFragments」ブログ投稿の DialogFragment でこれを行うための推奨される方法を読むことができます(読みやすくするために少し変更されています)。

// Show soft keyboard automatically
mEditText.requestFocus();
int mode = WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;
getDialog().getWindow().setSoftInputMode(mode);

これは、折りたたみ可能な EditText アクション ビューを展開してフォーカスをリクエストするときに機能しないようです。私は現在これを持っています。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.my_activity, menu);

    MenuItem menuItem = menu.findItem(R.id.menu_search);
    final EditText searchText = (EditText) menuItem.getActionView();

    menuItem.setOnActionExpandListener(new OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            return true; // Return true to collapse action view
        }

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            searchText.requestFocus();
            int mode = WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;
            getWindow().setSoftInputMode(mode);
            return true; // Return true to expand action view
        }
    });
}

my_activity メニュー xml ファイルでのメニュー項目の定義は次のとおりです。

<item
    android:id="@+id/menu_search"
    android:icon="@drawable/ic_menu_search"
    android:actionLayout="@layout/collapsible_edittext"
    android:showAsAction="always|collapseActionView"
    android:title="@string/menu_search"/>

... そして collapsible_edittext アクションレイアウト

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/hint_search"
    android:imeOptions="actionSearch"
    android:inputType="text"
    android:focusable="true"
    android:focusableInTouchMode="true" />

警告:ソフト キーボードを強制したくありません。ユーザーがフォーカスを要求するハードウェア キーボードを持っている場合は十分です。

誰か考えはありますか?

4

3 に答える 3

27

searchText がまだ展開されていないため、質問のコードで requestFocus が発生しなかった可能性がありますか? とにかく、わかりません...これは私にとってはうまくいきました:

@Override
public boolean onMenuItemActionExpand(MenuItem item) {
    searchText.post(new Runnable() {
        @Override
        public void run() {
            searchText.requestFocus();
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(searchText, InputMethodManager.SHOW_IMPLICIT);
        }
    });
    return true; // Return true to expand action view
}
于 2012-06-13T12:14:14.530 に答える
3

私にも効きます!

ActionViewを折りたたんだり、キーボードを非表示にしたりするには、次を使用します。

ソフトキーボードの検索ボタン(またはその他)をキャプチャするようにリスナーを設定してから、次を使用しますcollapseActionView()

searchText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            searchMenuItem.collapseActionView();
            return true;
        }
        return false;
    }
});

次に、メソッドにキーボード非表示コードを記述しonMenuItemActionCollapse()ます。

public boolean onMenuItemActionCollapse(MenuItem item) {
    // Do something when collapsed
    searchText.post(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(searchText.getWindowToken(), 0);
        }
    });
    return true; // Return true to collapse action view
}
于 2012-07-26T06:54:09.890 に答える
2

ActionBar で折りたたみ可能な検索項目を簡単に実装するにはSearchView、ActionBarSherlock ( com.actionbarsherlock.widget.SearchView) でも使用できる which を使用するだけです。

ビューを自動的に展開/折りたたむときにソフトウェアキーボードを表示/非表示にする多くの便利なメソッドとハンドルが付属しています。さらに、検索の送信時に何を行うかを定義する方が簡単です。その上で設定できるすべてのリスナーを見てくださいSearchView

次に例を示します。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    SearchView searchView = new SearchView(this);

    searchView.setQueryHint(getString(R.string.search_hint));
    searchView.setOnQueryTextListener(new OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {
            // what to do on submit, e.g. start an Activity and pass the query param
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }

    });

    menu.add(getString(R.string.search_title))
        .setIcon(R.drawable.ic_action_search)
        .setActionView(searchView)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM|MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    // ...place to add other menu items

    return super.onCreateOptionsMenu(menu);
}

別の正当な理由:EditText検索ビューとしてカスタムを使用すると、古いデバイスでレイアウトの問題が発生しました(質問で行ったように)。デフォルトSearchViewはこれらの問題をすべて解決し、多くのコード行を節約しました。

于 2013-01-08T20:50:09.643 に答える