【Android学习笔记】OnSearchRequest()调用搜索框仿QQ微信

这篇博客介绍了如何在Android应用中实现类似QQ和微信的隐藏式搜索功能。通过调用OnSearchRequested()方法,结合SearchManager,可以创建一个在顶部显示的系统搜索框。博主分享了包含SearchActivity、ResultActivity、SearchProvider和SearchUtil的代码示例,并提供了相关布局文件result.xml和AndroidManifest.xml的配置细节。
摘要由CSDN通过智能技术生成

     当我们用到搜索框时,有用过AutoCompleTextView来自动补全,这个很方便使用,但是我们有时候会想要QQ或者微信的那种隐藏起来的搜索功能,即searchManager,就是touch一下,在顶部出现搜索框,这个搜索框是系统自带的,我们直接调用就行,但是,实际上并不是这么简单。这里附上demo的代码

SearchActivty:

package com.example.kam_search;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class SearchActivty extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = this.getIntent();
		setContentView(R.layout.main);
		if (Intent.ACTION_VIEW.equals(intent.getAction())) {
			SearchUtil.Word theWord = SearchUtil.getInstance().getMatches(
					intent.getDataString().trim().toLowerCase()).get(0);
			launchWord(theWord);
			finish();
		} else {
		Button button = (Button) findViewById(R.id.button);
		button.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				onSearchRequested();
				return false;
			}
		});
	}
	}
	
	private void launchWord(SearchUtil.Word pavilion) {
		Intent next = new Intent();
		next.setClass(this, ResultActivty.class);
		Bundle bundle = new Bundle();
		bundle.putString("word", pavilion.word);
		next.putExtras(bundle);
		next.putExtras(bundle);
		startActivity(next);
	}
}


ResultActivty:

package com.example.kam_search;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ResultActivty extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
		
		Bundle bundle = this.getIntent().getExtras();
		String word = bundle.getString("word");
		
		TextView textView=(TextView)findViewById(R.id.test);
		
		textView.setText("结果:"+word);
	}
}

<

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值