android 仿QQ搜索框自动弹出、收起输入法

Home / Android MrLee 2015-3-20 9477

虽然是一个简单的功能,但是记录下来分享给大家!
在开发APP过程中少不了要使用到搜索功能,像手机QQ搜索,点一下弹出一个新的透明窗口并弹出输入法,点搜索结果自动收起输入法,那么这是怎么办到的呢,而且搜索框还附有向上的动画!
先看下我的效果图:

QQ截图20150320162809

点击事件触发代码
y = search_layout.getTop();
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, -y);
animation.setDuration(200);
animation.setFillAfter(true);
//显示移动动画
animation.setAnimationListener(new AnimationListener() {
	@Override
	public void onAnimationRepeat(Animation animation) {
	}
	@Override
	public void onAnimationStart(Animation animation) {
	}
	@Override
	public void onAnimationEnd(Animation animation) {
		Intent intent = new Intent(getActivity(),
				NewActivity2015.class);
		startActivityForResult(intent, 1000);
	}
});
getView().startAnimation(animation);//getView()为当前窗口最外层布局

//回到主界面动画
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1000) {
			TranslateAnimation animation = new TranslateAnimation(0, 0, -y, 0);
			animation.setDuration(520);
			animation.setFillAfter(true);
			getView().startAnimation(animation);
		}
	}

在弹出来的NewActivity2015中实现以下代码,分别是用来弹出键盘和关闭键盘的代码
@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (search_edit != null) {
			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.showSoftInput(search_edit, InputMethodManager.RESULT_SHOWN);
			imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
					InputMethodManager.HIDE_IMPLICIT_ONLY);
		}
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if (search_edit != null) {
			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(search_edit.getWindowToken(), 0);
		}
	}

是不是很简单呢!

本文链接:https://www.it72.com/1501.htm

推荐阅读
最新回复 (0)
返回