国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android編程自定義搜索框?qū)崿F(xiàn)方法【附demo源碼下載】

2019-10-22 18:18:02
字體:
供稿:網(wǎng)友

本文實例講述了Android編程自定義搜索框實現(xiàn)方法。分享給大家供大家參考,具體如下:

先來看效果圖吧~

Android,搜索框,源碼下載

分析:這只是模擬了一個靜態(tài)數(shù)據(jù)的刪除與顯示

用EditText+PopupWindow+listView實現(xiàn)的

步驟:

1.先寫出搜索框來-activity_mian布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  >  <EditText    android:id="@+id/et"    android:layout_width="match_parent"    android:layout_height="wrap_content"    />   <ImageView     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/click"     android:layout_alignParentRight="true"     android:src="@drawable/down_arrow"/></RelativeLayout>

效果:

Android,搜索框,源碼下載

2.數(shù)據(jù)的加載,把數(shù)據(jù)寫在ArrayList數(shù)組中,然后用適配器加載出來~

data=new ArrayList<String>();for(int i=0;i<20;i++){  data.add("1000"+i);}list.setAdapter(new MyAdapter());

3.點擊箭頭出現(xiàn)數(shù)據(jù),在EditText搜索框下面出現(xiàn),用PopupWindow實現(xiàn)~

@Overridepublic 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);      //點擊屏幕以外的區(qū)域會關(guān)掉      popup.setOutsideTouchable(true);      popup.setBackgroundDrawable(new ColorDrawable());     //顯示在哪個控件的下面      popup.showAsDropDown(et);    // }else{    //  popup=null;     //}     break;  default:    break;  }}

4.listview適配器加載數(shù)據(jù)并且點擊清除數(shù)據(jù)的圖片,數(shù)據(jù)會消失:

class MyAdapter extends BaseAdapter{@Overridepublic int getCount() {  // TODO Auto-generated method stub  if(data!=null){  return data.size();  }else {    return 0;  }}@Overridepublic Object getItem(int position) {  // TODO Auto-generated method stub  return null;}@Overridepublic long getItemId(int position) {  // TODO Auto-generated method stub  return 0;}@Overridepublic 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的點擊,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();  }});

這樣就實現(xiàn)了自定義搜索框~

完整MainActivity:

public class MainActivity extends Activity implements OnClickListener{  private ImageView click;  private EditText et;  private PopupWindow popup;  ListView list;  List<String>data;  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<String>();    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);        //點擊屏幕以外的區(qū)域會關(guān)掉        popup.setOutsideTouchable(true);        popup.setBackgroundDrawable(new ColorDrawable());       //顯示在哪個控件的下面        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"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  >  <ImageView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:src="@drawable/user"/>  <TextView    android:id="@+id/tv"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginLeft="100dp"/>  <ImageView    android:id="@+id/iv"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_alignParentRight="true"    android:src="@drawable/delete"/></RelativeLayout>

附:完整實例代碼點擊此處本站下載

希望本文所述對大家Android程序設(shè)計有所幫助。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 北川| 武夷山市| 红原县| 东明县| 蓝田县| 晋宁县| 丰宁| 洛阳市| 凤阳县| 五家渠市| 荆门市| 同仁县| 平定县| 蛟河市| 望奎县| 巴彦县| 鸡东县| 方山县| 囊谦县| 砚山县| 句容市| 玉门市| 黎川县| 油尖旺区| 双桥区| 黑龙江省| 当阳市| 岑巩县| 方正县| 鄂托克旗| 锡林浩特市| 横峰县| 如东县| 房产| 财经| 什邡市| 商南县| 土默特右旗| 普兰店市| 桃源县| 徐汇区|