本文實(shí)例講述了Android編程自定義搜索框?qū)崿F(xiàn)方法。分享給大家供大家參考,具體如下:
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比合陽網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式合陽網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋合陽地區(qū)。費(fèi)用合理售后完善,10年實(shí)體公司更值得信賴。
先來看效果圖吧~
分析:這只是模擬了一個(gè)靜態(tài)數(shù)據(jù)的刪除與顯示
用EditText+PopupWindow+listView實(shí)現(xiàn)的
步驟:
1.先寫出搜索框來-activity_mian布局:
效果:
2.數(shù)據(jù)的加載,把數(shù)據(jù)寫在ArrayList數(shù)組中,然后用適配器加載出來~
data=new ArrayList(); for(int i=0;i<20;i++){ data.add("1000"+i); } list.setAdapter(new MyAdapter());
3.點(diǎn)擊箭頭出現(xiàn)數(shù)據(jù),在EditText搜索框下面出現(xiàn),用PopupWindow實(shí)現(xiàn)~
@Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.click: //if(popup==null){ /*TextView tv=new TextView(this); tv.setText("123243");*/ list.setAdapter(new MyAdapter()); popup=new PopupWindow(list, et.getWidth(), 500); popup.setFocusable(true); //點(diǎn)擊屏幕以外的區(qū)域會(huì)關(guān)掉 popup.setOutsideTouchable(true); popup.setBackgroundDrawable(new ColorDrawable()); //顯示在哪個(gè)控件的下面 popup.showAsDropDown(et); // }else{ // popup=null; //} break; default: break; } }
4.listview適配器加載數(shù)據(jù)并且點(diǎn)擊清除數(shù)據(jù)的圖片,數(shù)據(jù)會(huì)消失:
class MyAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub if(data!=null){ return data.size(); }else { return 0; } } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View view=View.inflate(MainActivity.this, R.layout.listview, null); TextView tv=(TextView) view.findViewById(R.id.tv); ImageView iv=(ImageView) view.findViewById(R.id.iv); text=data.get(position); tv.setText(text); iv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub data.remove(text); notifyDataSetChanged(); } }); return view; } }
5.listview的點(diǎn)擊,PopupWindow的消失,EditText數(shù)據(jù)的顯示:
list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub et.setText(text); et.setSelection(text.length());//光標(biāo)在text的后面 //PopupWindow消失 popup.dismiss(); } });
這樣就實(shí)現(xiàn)了自定義搜索框~
完整MainActivity:
public class MainActivity extends Activity implements OnClickListener{ private ImageView click; private EditText et; private PopupWindow popup; ListView list; Listdata; String text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et=(EditText) findViewById(R.id.et); click=(ImageView) findViewById(R.id.click); click.setOnClickListener(this); list=new ListView(this); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub et.setText(text); et.setSelection(text.length());//光標(biāo)在text的后面 //PopupWindow消失 popup.dismiss(); } }); data=new ArrayList (); for(int i=0;i<20;i++){ data.add("1000"+i); } } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.click: //if(popup==null){ /*TextView tv=new TextView(this); tv.setText("123243");*/ list.setAdapter(new MyAdapter()); popup=new PopupWindow(list, et.getWidth(), 500); popup.setFocusable(true); //點(diǎn)擊屏幕以外的區(qū)域會(huì)關(guān)掉 popup.setOutsideTouchable(true); popup.setBackgroundDrawable(new ColorDrawable()); //顯示在哪個(gè)控件的下面 popup.showAsDropDown(et); // }else{ // popup=null; //} break; default: break; } } class MyAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub if(data!=null){ return data.size(); }else { return 0; } } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View view=View.inflate(MainActivity.this, R.layout.listview, null); TextView tv=(TextView) view.findViewById(R.id.tv); ImageView iv=(ImageView) view.findViewById(R.id.iv); text=data.get(position); tv.setText(text); iv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub data.remove(text); notifyDataSetChanged(); } }); return view; } } }
listview布局:
<?xml version="1.0" encoding="utf-8"?>
附:完整實(shí)例代碼點(diǎn)擊此處本站下載。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。