亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2017年12月29日 15:29:36   作者:飄走的我  
這篇文章主要介紹了Android編程自定義搜索框?qū)崿F(xiàn)方法,涉及Android界面布局、數(shù)據(jù)加載、事件響應(yīng)等相關(guān)操作技巧,并附帶完整demo源碼供讀者下載參考,需要的朋友可以參考下

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

先來看效果圖吧~

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

用EditText+PopupWindow+listView實(shí)現(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>

效果:

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.點(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ū)域會關(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ù)會消失:

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;
  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);
        //點(diǎn)擊屏幕以外的區(qū)域會關(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"?>
<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>

附:完整實(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é)

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

相關(guān)文章

  • Android用 Mob 實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼實(shí)例

    Android用 Mob 實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼實(shí)例

    這篇文章主要介紹了Android用 Mob 實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 理解Android系統(tǒng)Binder機(jī)制

    理解Android系統(tǒng)Binder機(jī)制

    這篇文章主要為大家介紹了Android系統(tǒng)Binder機(jī)制,幫助大家理解Binder機(jī)制,感興趣的朋友可以參考一下
    2016-05-05
  • 詳解關(guān)于AndroidQ獲取不到imsi解決方案

    詳解關(guān)于AndroidQ獲取不到imsi解決方案

    這篇文章主要介紹了詳解關(guān)于AndroidQ獲取不到imsi解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Flutter?Widget?之package?mason實(shí)現(xiàn)詳解

    Flutter?Widget?之package?mason實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了Flutter?Widget?之package:?mason實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Android下拉刷新上拉加載更多左滑動(dòng)刪除

    Android下拉刷新上拉加載更多左滑動(dòng)刪除

    本文給大家分享一段代碼實(shí)現(xiàn)Android下拉刷新上拉加載更多仿ios左滑動(dòng)刪除item,非常實(shí)用,代碼簡單易懂,特此分享腳本之家平臺供大家學(xué)習(xí)
    2016-01-01
  • Flutter懸浮按鈕FloatingActionButton使用詳解

    Flutter懸浮按鈕FloatingActionButton使用詳解

    本文主要介紹了Flutter懸浮按鈕FloatingActionButton使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • Android 阿里云OSS文件上傳的實(shí)現(xiàn)示例

    Android 阿里云OSS文件上傳的實(shí)現(xiàn)示例

    這篇文章主要介紹了Android 阿里云OSS文件上傳的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Android中復(fù)制圖片的實(shí)例代碼

    Android中復(fù)制圖片的實(shí)例代碼

    本文通過實(shí)例代碼給大家介紹了android 復(fù)制圖片的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-08-08
  • ScrollView嵌套ListView滑動(dòng)沖突的解決方法

    ScrollView嵌套ListView滑動(dòng)沖突的解決方法

    這篇文章主要介紹了ScrollView嵌套ListView滑動(dòng)沖突的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android中自定義對話框(Dialog)的實(shí)例代碼

    Android中自定義對話框(Dialog)的實(shí)例代碼

    這篇文章介紹了Android中自定義對話框(Dialog)的實(shí)例代碼,有需要的朋友可以參考一下
    2013-08-08

最新評論