如何使用 SearchView 工具栏中Android

如何使用 SearchView 工具栏中Android,第1张

1、SearchView是搜索框组件,它可以让用户在文本框里输入文字,通过监听器取得用户的输入,当用户点击搜索时,监听器执行实际的搜索。

2、SearchView组件的常用方法如下:

①setIconifiedByDefault(boolean iconified) ===>设置搜索框默认是否自动缩小为图标。

②setOnQueryTextListener(SearchView,OnQueryTextListener listener) ===>为搜索框设置监听器

③setSubmitButtonEnabled(boolean enabled) ===>设置是否显示搜索按钮

④setQueryHint(CharSequence hint) ===>设置搜索框内的默认显示的提示文本

3、为SearchView增加一个配套的ListView,则可以为其增加自动完成的功能,即ListView用于为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.ScheduledExecutorService

import java.util.concurrent.ScheduledFuture

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 MyHandlerhandler

// schedule executor

private ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(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

schedule(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 ScheduledFuture<?>schedule(Runnable command, long delayTimeMills) {

return scheduledExecutor.schedule(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放到ScheduledExecutorService中,500ms后执行,在线程执行时判断当前输入框内容和要搜索内容,若相等则继续执行,否则直接返回,避免不必要的数据获取和多个搜索提示同时出现。

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE

| WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)

表示默认输入法d出

编辑框内容为空点击取消的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的类名

方法一、从组件中把按钮拉进你在放的地方(舞台或者元件中)

如果直接设置,你可以点击按钮,然后设置属性。然后组件参数。有一行属性名为label.在后面输入你要的名字。就可以了。

方法二、从组件中把按钮拉到舞台上,然后删除。

import fl.controls.Button

import flash.events.MouseEvent

var btn:Button=new Button()

btn.x=100//自己设置x坐标

btn.y=100//

btn.width=50//宽度

btn.height=30//高度

btn.label="标签名"//换成自己想要的标签名。

addChild(btn)//或者是mc.addChild(btn)mc为元件实例名

btn.addEventListener(MouseEvent.CLICK,cli)//监听单击事件

function cli(e:MouseEvent):void

{

trace(btn.label)//打印出按钮的标签名

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11851210.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存