当我们用到搜索框时,有用过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);
}
}
<