Android-工具栏中的SearchView [英] Android- SearchView in Toolbar
本文介绍了Android-工具栏中的SearchView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想在我的app中添加searchview.但是我不想搜索任何东西,只要我希望用户输入查询即可.到目前为止,我尝试了这段代码,但是当我运行我的应用程序时,它崩溃了.
i want to just add searchview in my app.but i dont want to search any thing just i want the query entered by the user. so far i tried this code but when i run my app it crashes.
更新:我尝试了此操作,但是尽管我的应用程序崩溃了.
Update: I tried this one but eventhough my app crashes.
main_menu.xml代码
main_menu.xml code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:title="Search"
android:icon="@android:drawable/ic_menu_search"
app:actionViewClass="android.support.v7.widget.SerachView"
app:showAsAction="always"
/>
MainActivity.java
MainActivity.java
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.main_menu,menu);
SearchView searchView=(SearchView)findViewById(R.id.search);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// Toast.makeText(this,query,Toast.LENGTH_LONG).show();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
请问有人可以帮助我解决此问题吗?谢谢.
Can Anyone help me to solve this problem please. ThankYou.
推荐答案
在您的 menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".HomeActivity">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
</menu>
并将此简单代码添加到您的java中:
and add this simple code to your java :
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
// Not implemented here
return false;
default:
break;
}
searchView.setOnQueryTextListener(queryTextListener);
return super.onOptionsItemSelected(item);
}
这篇关于Android-工具栏中的SearchView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文