searchviewandroid
㈠ android searchview的问题
java">(StringnewText){
//dosomething...
//在这里根据newText对listView的数据进行过滤,然后更新
}
㈡ android searchview 怎么改变默认的搜索图标
Framework中SearchView的属性如下:
<attr name="searchIcon" format="reference" />对应搜索图标
在SearchView中直接指定即可
<SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:searchIcon="@drawable/custom_search_icon"/>
㈢ 如何使用 SearchView 工具栏中Android
众所周知,在使用ActionBar的时候,一堆的问题:这个文字能不能定制,位置能不能改变,图标的间距怎么控制神马的,由此暴露出了ActionBar设计的不灵活。为此官方提供了ToolBar,并且提供了supprot library用于向下兼容。Toolbar之所以灵活,是因为它其实就是一个ViewGroup,我们在使用的时候和普通的组件一样,在布局文件中声明。
Part1:ToolBar的引入step1:设置style主题,主要任务是去除原本的ActionBar
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar"><!--Light.DarkActionBar表示默认的黑色主体的Actionbar-->
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/sky_blue</item>
<item name="colorPrimaryDark">@color/deep_blue</item>
<item name="colorAccent">@color/material_deep_teal_200</item>
<item name="android:textColorPrimary">@color/white</item>
</style>
colorPrimary表示标题栏ActionBar的颜色;colorPrimaryDark表示状态栏的颜色; colorAccent表示输入框,按钮等被选中时的颜色; textColorPrimary表示标题栏(ActionBar或者ToolBar)中字体的颜色
当然啦,第一步的实现也可以在程序代码中或者style里静态或者动态地去掉ActionBar
step2:在你需要引入ToolBar的布局文件中引入ToolBar:
<android.support.v7.widget.Toolbar
android:id="@+id/main_toolbar"
android:theme="@style/ThemeOverlay.AppCompat.Dark"//这里的主题可以用来反衬toolBar的overFlow颜色
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/sky_blue"
></android.support.v7.widget.Toolbar>
因为colorPrimary是给ActionBar设定颜色的,因此如果我们使用ToolBar,就需要自己去设定ToolBar的背景颜色
setp3:在代码中获取ToolBar控件以及实例化:
Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
toolbar.setNavigationIcon(R.mipmap.ic_toc_white_24dp);//设置ToolBar头部图标
toolbar.setTitle("ToolBar");//设置标题,也可以在xml中静态实现
setSupportActionBar(toolbar);//使活动支持ToolBar
Part2:ToolBar里各SearchView的引入:
setp1:在menu/xxxx.xml的菜单布局文件将SearchView以菜单条目的方式加入到ToolBar中
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"
android:inputType="textCapWords"
android:imeOptions="actionSearch"
android:title="search" />
这中添加条目的方式和添加普通控件的方式是一样的,这里需要对showAsAction具体说明以下:
ifRoom表示当toolBar空间足够时,显示图标在标题栏中,否则将它隐藏到ToolBar末端的overFlow中,点开overFlow只显示item的title
CollapseActionView表示当前空间点开之后占据整个ToolBar空间
always表示总是显示在标题栏中,当我们长按该item后,就会以Toast的方式显示出它的title
never表示总是隐藏在overFlow中
step2:在Java程序代码中实例SearchView
覆写onCreateOptionsMenu方法,为什么要在这个方法里实现对SearchView的实例化呢?因为toolBar里的点击事件都以菜单的形式实现的,如果我们需要让它隐藏到overFlow中,并且点击菜单键并唤出,那么就只需要把它的显示方式设置为never即可。
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);//在菜单中找到对应控件的item
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
Log.d("Tag", "menu create");
MenuItemCompat.setOnActionExpandListener(menuItem, new MenuItemCompat.OnActionExpandListener() {//设置打开关闭动作监听
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
Toast.makeText(MainActivity.this, "onExpand", Toast.LENGTH_LONG).show();
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
Toast.makeText(MainActivity.this, "Collapse", Toast.LENGTH_LONG).show();
return true;
}
});
return super.onCreateOptionsMenu(menu);
㈣ 如何使用 SearchView 工具栏中Android
SearchView是android系统中内置的一个搜索框组件,可以很方便在添加在用户界面之上,SearchView的使用:
1.创建布局 main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SearchView
android:id="@+id/sv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:imeOptions="actionGo" />
</LinearLayout>
2.在显示suggestion的时候会用到下面的布局文件:mytextview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50sp"
android:orientation="vertical" >
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="5sp"
android:textSize="18sp" />
</LinearLayout>
3.java中的使用代码:main.java:
package xxx.xxx.xxx;
import java.lang.reflect.Field;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class Main extends Activity {
SearchView sv = null;
ListView lv = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sv = (SearchView) this.findViewById(R.id.sv);
sv.setIconifiedByDefault(false);
sv.setSubmitButtonEnabled(true);
sv.setQueryHint("查询");
//通过反射,修改默认的样式,可以从android的search_view.xml中找到需要的组件
try {
Field field = sv.getClass().getDeclaredField("mSubmitButton");
field.setAccessible(true);
ImageView iv = (ImageView) field.get(sv);
iv.setImageDrawable(this.getResources().getDrawable(
R.drawable.pointer));
} catch (Exception e) {
e.printStackTrace();
}
Cursor cursor = this.getTestCursor();
@SuppressWarnings("deprecation")
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.mytextview, cursor, new String[] { "tb_name" },
new int[] { R.id.textview });
sv.setSuggestionsAdapter(adapter);
sv.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String str) {
return false;
}
@Override
public boolean onQueryTextSubmit(String str) {
Toast.makeText(Main.this, str, Toast.LENGTH_SHORT).show();
return false;
}
});
}
//添加suggestion需要的数据
public Cursor getTestCursor() {
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(
this.getFilesDir() + "/my.db3", null);
Cursor cursor = null;
try {
String insertSql = "insert into tb_test values (null,?,?)";
db.execSQL(insertSql, new Object[] { "aa", 1 });
db.execSQL(insertSql, new Object[] { "ab", 2 });
db.execSQL(insertSql, new Object[] { "ac", 3 });
db.execSQL(insertSql, new Object[] { "ad", 4 });
db.execSQL(insertSql, new Object[] { "ae", 5 });
String querySql = "select * from tb_test";
cursor = db.rawQuery(querySql, null);
} catch (Exception e) {
String sql = "create table tb_test (_id integer primary key autoincrement,tb_name varchar(20),tb_age integer)";
db.execSQL(sql);
String insertSql = "insert into tb_test values (null,?,?)";
db.execSQL(insertSql, new Object[] { "aa", 1 });
db.execSQL(insertSql, new Object[] { "ab", 2 });
db.execSQL(insertSql, new Object[] { "ac", 3 });
db.execSQL(insertSql, new Object[] { "ad", 4 });
db.execSQL(insertSql, new Object[] { "ae", 5 });
String querySql = "select * from tb_test";
cursor = db.rawQuery(querySql, null);
}
return cursor;
}
}
㈤ android SearchView 输入后按返回键无法返回
android searchView输入后按返回键无法返回可能的原因如下:
当android 的代码中,没有对返回键进行监听,用户点击返回键是不会有事件响应的。
android searchView该控件存在bug,输入之后,停留在该界面,无法跳转。
㈥ android SearchView设置默认值
setQuery("2",false);
㈦ android SearchView 输入后按返回键无法返回
android
searchView输入后按返回键无法返回可能的原因如下:
1.
当android
的代码中,没有对返回键进行监听,用户点击返回键是不会有事件响应的。
2.
android
searchView该控件存在bug,输入之后,停留在该界面,无法跳转。
㈧ android怎么使用SearchView啊
SearchView即时搜索提示功能,主要实现如下:
layout布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:iconifiedByDefault="true"
android:inputType="textCapWords"
android:imeOptions="actionSearch"
android:queryHint="" />
</RelativeLayout>
xml中主要配置有四个属性,如下:
android:iconifiedByDefault表示搜索图标是否在输入框内。true效果更加
android:imeOptions设置IME options,即输入法的回车键的功能,可以是搜索、下一个、发送、完成等等。这里actionSearch表示搜索
android:inputType输入框文本类型,可控制输入法键盘样式,如numberPassword即为数字密码样式
android:queryHint输入框默认文本
SearchView的几个主要函数
setOnCloseListener(SearchView.OnCloseListener listener)表示点击取消按钮listener,默认点击搜索输入框
setOnQueryTextListener(SearchView.OnQueryTextListener listener)表示输入框文字listener,包括public boolean onQueryTextSubmit(String query)开始搜索listener,public boolean onQueryTextChange(String newText)输入框内容变化listener,两个函数
java部分实现:
package cn.trinea.android.demo;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheledExecutorService;
import java.util.concurrent.ScheledFuture;
import java.util.concurrent.TimeUnit;
import android.app.ActionBar;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.SearchView;
import android.widget.SearchView.OnCloseListener;
import android.widget.Toast;
public class SearchViewDemo extends Activity {
private SearchView searchView;
private Context context;
private MyHandler handler;
// schele executor
private ScheledExecutorService scheledExecutor = Executors.newScheledThreadPool(10);
private String currentSearchTip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_view_demo);
context = getApplicationContext();
handler = new MyHandler();
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_HOME_AS_UP
| ActionBar.DISPLAY_SHOW_CUSTOM);
setTitle(" ");
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customActionBarView = inflater.inflate(R.layout.search_view_demo_title, null);
searchView = (SearchView)customActionBarView.findViewById(R.id.search_view);
searchView.setIconified(false);
searchView.setOnCloseListener(new OnCloseListener() {
@Override
public boolean onClose() {
// to avoid click x button and the edittext hidden
return true;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
public boolean onQueryTextSubmit(String query) {
Toast.makeText(context, "begin search", Toast.LENGTH_SHORT).show();
return true;
}
public boolean onQueryTextChange(String newText) {
if (newText != null && newText.length() > 0) {
currentSearchTip = newText;
showSearchTip(newText);
}
return true;
}
});
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL
| Gravity.RIGHT);
actionBar.setCustomView(customActionBarView, params);
// show keyboard
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
public void showSearchTip(String newText) {
// excute after 500ms, and when excute, judge current search tip and newText
schele(new SearchTipThread(newText), 500);
}
class SearchTipThread implements Runnable {
String newText;
public SearchTipThread(String newText){
this.newText = newText;
}
public void run() {
// keep only one thread to load current search tip, u can get data from network here
if (newText != null && newText.equals(currentSearchTip)) {
handler.sendMessage(handler.obtainMessage(1, newText + " search tip"));
}
}
}
public ScheledFuture<?> schele(Runnable command, long delayTimeMills) {
return scheledExecutor.schele(command, delayTimeMills, TimeUnit.MILLISECONDS);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
onBackPressed();
return true;
}
}
return false;
}
private class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Toast.makeText(context, (String)msg.obj, Toast.LENGTH_SHORT).show();
break;
}
}
}
}
上面代码在onQueryTextChange函数即输入框内容每次变化时将一个数据获取线程SearchTipThread放到ScheledExecutorService中,500ms后执行,在线程执行时判断当前输入框内容和要搜索内容,若相等则继续执行,否则直接返回,避免不必要的数据获取和多个搜索提示同时出现。
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
| WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
表示默认输入法弹出
编辑框内容为空点击取消的x按钮,编辑框收缩,可在onClose中返回true
Java
1
2
3
4
5
6
7
searchView.setOnCloseListener(new OnCloseListener() {
@Override
public boolean onClose() {
return true;
}
});
searchView.onActionViewExpanded();表示在内容为空时不显示取消的x按钮,内容不为空时显示.
searchView.setSubmitButtonEnabled(true);编辑框后显示search按钮,个人建议用android:imeOptions=”actionSearch”代替。
隐藏输入法键盘
InputMethodManager inputMethodManager;
inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
private void hideSoftInput() {
if (inputMethodManager != null) {
View v = SearchActivity.this.getCurrentFocus();
if (v == null) {
return;
}
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
searchView.clearFocus();
}
}
其中SearchActivity为Activity的类名
㈨ android searchview 怎么改变默认的搜索图标
您好,很高兴能帮助您,
在res/drawable-hdpi或res/drawable-ldpi或res/drawable-mdpi目录下,加下你要显示的图片,最好后缀是为.png的,然后修改AndroidManifest.xml文件,里面有这样一行代码<application android:icon="@drawable/icon" android:label="@string/app_name">,把它改为<application android:icon="@drawable/***" android:label="@string/app_name">(“***”表示你放入的图片的名,比如你放入app.png,那么就是android:icon="@drawable/app")
你的采纳是我前进的动力,还有不懂的地方,请你继续“追问”!
如你还有别的问题,可另外向我求助;答题不易,互相理解,互相帮助!