我有一个searchView,看起来像这样:
private void setupSearchView() {
mSearchView = (SearchView) getActivity().findViewById(
R.id.search_view_neue);
setSearchViewBackground();
mSearchView.setOnClickListener(this);
mSearchView.setOnQueryTextListener(this);
}
public boolean onQueryTextSubmit(String query) {
searchcounter = searchcounter + 1;
setSearchViewBackground();
ArrayList<WissensdokumenteRecord> documents = settingListContent(new ArrayList<WissensdokumenteRecord>());
setListAdapter(new NeueWissensdokumentItemAdapter(
inflater.getContext(), R.layout.row_example, documents));
InputMethodManager im = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(getActivity().getCurrentFocus()
.getWindowToken(), 0);
return false;
}
<SearchView
android:id="@+id/search_view_neue"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:background="@drawable/border_searchview"
android:maxWidth="540dp"
android:queryHint="Neue Dokumente suchen" >
</SearchView>
Run Code Online (Sandbox Code Playgroud)
因此,搜索视图的行为是,通过单击搜索按钮打开键盘.现在我可以搜索一些东西了.是否可以通过点击搜索视图中的任意位置进行搜索?有没有人有我的榜样?
这样做时我总是得到textView为null:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat
.getActionView(searchItem);
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) searchView.findViewById(id);
textView.setTextColor(Color.WHITE);
}
Run Code Online (Sandbox Code Playgroud)
谁知道为什么?
我想让我的应用程序包含一个单一的活动.此活动应该能够创建搜索并接收搜索.不幸的是,当我点击操作栏中的搜索按钮时,我的SearchView中会出现一个"双"搜索栏.我的意思是有一个搜索栏(dark-- SearchView)在操作栏中显示一秒钟,然后在操作栏上显示第二个(白色).有帮助吗?我究竟做错了什么?对不起,这个搜索的东西都是新的,让我感到困惑.
MainActivity(唯一的活动):
public class MainActivity extends ActionBarActivity {
Menu mMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//getSupportActionBar().setDisplayShowTitleEnabled(false);
setContentView(R.layout.activity_main);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
}
}
@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
mMenu = menu;
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
if …Run Code Online (Sandbox Code Playgroud) SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(Menus.SEARCH));
searchView.setQueryHint(this.getString(R.string.search));
editSearch = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
editSearch.setHintTextColor(getResources().getColor(R.color.white));
searchView.setOnQueryTextListener(OnQuerySearchView);
private OnQueryTextListener OnQuerySearchView = new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String newText) {
if (TextUtils.isEmpty(newText)) {
listAllContact.clearTextFilter();
} else {
listAllContact.setFilterText(newText.toString());
}
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
String text = editSearch.getText().toString()
.toLowerCase(Locale.getDefault());
adapter.filter(text);
return true;
}
};
Run Code Online (Sandbox Code Playgroud) 我正在使用工具栏创建一个searchview onCreateOptionsMenu,但无法获得X最初为白色的清除按钮.开始输入字母时变为白色.清理后它也会保持白色.
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.responsible_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); //TODO: May not be needed?
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextSubmit(String query)
{
mAdapter.updateUIWithFilter(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText)
{
mAdapter.updateUIWithFilter(newText);
return false;
}
});
// Does not work! Still not white.
ImageView searchClose = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchClose.setColorFilter(Color.argb(255, 255, 255, 255));
searchClose.setAlpha(255);
return true;
}
Run Code Online (Sandbox Code Playgroud)
responsible_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用了搜索视图.我想知道当我点击搜索视图上的搜索按钮时如何隐藏键盘?我必须使用后退按钮来查看结果.
我正在使用appcompat库和工具栏,我在我的应用程序中实现了一个搜索视图.但我希望在展开时删除搜索视图图标.
这是我的菜单xml文件:
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search"
android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/>
Run Code Online (Sandbox Code Playgroud)
这就是我给菜单充气的方式:
getMenuInflater().inflate(R.menu.main, menu);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
searchView.setQueryHint(getString(R.string.search_hint));
return super.onCreateOptionsMenu(menu);
Run Code Online (Sandbox Code Playgroud)
这就是我要删除的内容:

android android-appcompat android-actionbar searchview android-actionbar-compat
我正在为我的应用程序的搜索界面添加语音搜索功能.目前,它看起来像这样:
为了添加语音搜索,我voiceSearchMode在我的可搜索配置中添加了一个,它在SearchView中添加了一个按钮来触发语音对话框.界面现在看起来像这样:
但是,正如您在键盘上看到的那样,,麦克风按钮现已禁用.我找不到任何关于如何再打开它的文档,唯一相关的问题解释了如何明确禁用它.
是否可以同时使用系统的语音搜索对话框和键盘的语音输入SearchView?
如果搜索视图是打开的,我有一些设置布尔值的代码.
MenuItemCompat.setOnActionExpandListener(action_search, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item){
isSearch = true;
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item){
isSearch = false;
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
有用.我很满意它的反应方式.但是,它显示为已弃用,setOnActionExpandListener将与警告一起划掉
不推荐使用android.support.v4.view.MenuItemCompat.setOnActionExpandListener
建议?
我SearchView在其中一种布局中添加,并在某些设备上出现了崩溃。问题是abc_textfield_search_material找不到资源。但是我没有直接使用此资源,因为我的xml布局看起来像这样:
<androidx.appcompat.widget.SearchView
android:id="@+id/svSearch"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false"
android:queryHint="@string/filter"
app:iconifiedByDefault="false"
android:paddingBottom="4dp"
app:queryHint="@string/filter" />
Run Code Online (Sandbox Code Playgroud)
任何想法如何解决这个问题?仅当稀有设备(少于1%)发生此崩溃时,大多数设备才能正常工作。我已经在考虑将其改为TextView使用复合可绘制对象,但是也许有比不使用A更好的解决方案了SearchView
堆栈跟踪
java.lang.RuntimeException: Unable to pause activity {com.my.app/com.my.app.MainActivity}: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class androidx.appcompat.widget.SearchView
at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:4071)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4026)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3978)
at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:45)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1818)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6744)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line …Run Code Online (Sandbox Code Playgroud)