Android的 - 不能使用Android的支持库时,里面SearchWidget发现的TextView [英] Android - cannot find TextView inside SearchWidget when using Android Support library

查看:1008
本文介绍了Android的 - 不能使用Android的支持库时,里面SearchWidget发现的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的代码片段找到TextView的搜索查看里面的部件。

I used following snippet to find TextView inside SearchView widget.

    int autoCompleteTextViewID = getResources().getIdentifier("android:id/search_src_text", null, null);
    mQueryTextView = (AutoCompleteTextView) searchView.findViewById(autoCompleteTextViewID);

但是切换到 android.support.v7.appcompat 支持库的时候,它不工作了。

However when switching to android.support.v7.appcompat support library, it does not work any more.

我想这是因为支持库不使用的android: preFIX为机器人:ID / search_src_text
但我不知道它应该是。我试过

I guess it is because support library does not use android: prefix for "android:id/search_src_text", but I have no idea what should it be. I tried

getResources().getIdentifier("android.support.v7.appcompat:id/search_src_text", null, null);

P.S。更多code片段:

P.S. More code snippets:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        // this xml has funshion:actionViewClass="android.support.v7.widget.SearchView"
        getMenuInflater().inflate(R.menu.search_activity_actions_v7, menu);

        android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView) 
                MenuItemCompat.getActionView(menu.findItem(R.id.action_search_v7));
        if (searchView==null){
            FSLogcat.e(TAG, "searchView is null!");
        }else{
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(false);
            //searchView.requestFocus();
            searchView.setSubmitButtonEnabled(true);

            findSearchViewTextView(searchView);
            updateSearchViewText();
        }   

    return super.onCreateOptionsMenu(menu);
}

private void findSearchViewTextView(View searchView) {
    int autoCompleteTextViewID = getResources().getIdentifier("android:id/search_src_text", null, null);
    mQueryTextView = (AutoCompleteTextView) searchView.findViewById(autoCompleteTextViewID);
}

private void updateSearchViewText() {       
    if (mQueryTextView == null){
    } else {    
        mQueryTextView.setText(mQuery);
    }

}

注意搜索查看插件使得它很难得到建议,内部的TextView放在

Note that SearchView widget makes it hard to get suggestions to be put inside TextView

//      in SearchAutoComplete
//        /**
//         * We override this method to avoid replacing the query box text when a
//         * suggestion is clicked.
//         */
//        @Override
//        protected void replaceText(CharSequence text) {
//        }

更新:需要SearchWidget操作抵达SearchResultsActivity得到了SearchWidget为SearchResultsActivity后。虽然可能是应为下一次迭代一项活动,为当前版本将于本周实现我只需要解决使用率问题,即要确保的TextView里面SearchWidget上SearchResultsActivity总是有最新的查询。结果
也就是说,关键是code,如果它打破了,它就会被改写,但绝对没有通过克隆标准部件。本应该是其他方式。

UPDATE: The need for SearchWidget manipulation arrived after SearchResultsActivity got the SearchWidget as SearchResultsActivity. While possibly the should be implemented as one Activity in the next iteration, for current release due in this week I just need to solve usage issue i.e. to make sure that TextView inside SearchWidget on SearchResultsActivity always has the latest query.
That is, it is critical code if it breaks, it will be rewritten, but definitely not by cloning standard widget. The should be other way.

推荐答案

随着佩德罗·奥利维拉建议

As Pedro Oliveira suggested

mQueryTextView = (AutoCompleteTextView) searchView.findViewById(R.id.search_src_text);

单行只是工作。

这篇关于Android的 - 不能使用Android的支持库时,里面SearchWidget发现的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆