【问题标题】:Android - cannot find TextView inside SearchWidget when using Android Support libraryAndroid - 使用 Android 支持库时无法在 SearchWidget 中找到 TextView
【发布时间】:2014-11-02 18:32:06
【问题描述】:

我使用以下 sn-p 在 SearchView 小部件中查找 TextView。

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

但是当切换到android.support.v7.appcompat 支持库时,它就不再起作用了。

我猜是因为支持库没有为“android:id/search_src_text”使用android:前缀, 但我不知道应该是什么。我试过了

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

附:更多代码sn-ps:

@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);
    }

}

请注意,SearchView 小部件很难将建议放入 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) {
//        }

更新:在 SearchResultsActivity 将 SearchWidget 作为 SearchResultsActivity 之后,就需要对 SearchWidget 进行操作了。虽然可能应该在下一次迭代中作为一个 Activity 实现,但对于本周到期的当前版本,我只需要解决使用问题,即确保 SearchResultsActivity 上 SearchWidget 内的 TextView 始终具有最新查询。
也就是说,如果它是关键代码,它会被重写,但绝对不是通过克隆标准小部件。应该是其他方式。

【问题讨论】:

  • 尝试改用AutoCompleteTextView mQueryTextView = (AutoCompleteTextView) mSearchView.findViewById(R.id.search_src_text);
  • 正确的答案是你 fork SearchView 并将你想要的内容添加到你自己的副本中,而不是假设 Google 永远不会以破坏你代码的方式改变他们的实现。
  • 好吧,最好的办法是始终派生支持库并使用它而不是谷歌的。 ;)
  • 在问题中添加了更新
  • 你试过用 R.id.search_src_text 代替吗?

标签: android search android-support-library searchview


【解决方案1】:

正如佩德罗·奥利维拉建议的那样

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

单线刚刚好。

【讨论】:

  • 不适合我。仍然为空:kotlin.TypeCastException:null 不能在 org.example.appname.MainActivity.onCreateOptionsMenu(MainActivity.kt:78) 处转换​​为非空类型 android.widget.AutoCompleteTextView
【解决方案2】:

kotlin 一行

mQueryTextView = findViewById<AutoCompleteTextView>(androidx.appcompat.R.id.search_src_text)

【讨论】:

    【解决方案3】:

    你可以使用 Resource#getSystem 来获取视图

    Resources.getSystem().getIdentifier("search_src_text",
        "id", "android")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多