欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android实现简单实用的搜索框

时间:2022-08-12 10:03:57|栏目:Android代码|点击:

本文实例为大家分享了Android实现搜索框展示的具体代码,供大家参考,具体内容如下

展示效果

这里写图片描述

代码区

SouActivity

public class SouActivity extends AppCompatActivity implements TextWatcher{
  @BindView(R.id.app_sou)
  EditText appSou;
  @BindView(R.id.app_sou_list)
  ListView appSouList;
  @BindView(R.id.activity_sou)
  RelativeLayout activitySou;
  private String mUrl = "http://120.27.23.105/product/searchProducts";
  private List<MySouFr.DataBean> sdata;
  private MyBase myBase;
  private String asou;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sou);
    ButterKnife.bind(this);
    sdata=new ArrayList<MySouFr.DataBean>();
    appSou.addTextChangedListener(this);
    appSou.setOnFocusChangeListener(new View.OnFocusChangeListener() {
      @Override
      public void onFocusChange(View view, boolean b) {
        if(b)
        {
          appSou.setText("");
        }
      }
    });


  }

  @Override
  public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

  }

  @Override
  public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    //获取输入框的值
    asou = appSou.getText().toString().trim();

      OkHttp3Utils.getInstance().doGet(mUrl + "?keywords=" + asou + "&page=1", new GsonObjectCallback<MySouFr>() {

        @Override
        public void onUi(final MySouFr mySouFr) {
          /*适配器*/
          if (asou !=null&&!asou.equals("")) {
            sdata = mySouFr.getData();
            myBase = new MyBase();
            appSouList.setAdapter(myBase);
            appSouList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(SouActivity.this, Sou_item_Activity.class);
                intent.putExtra("url",mySouFr.getData().get(i).getDetailUrl());
                startActivity(intent);
//                Toast.makeText(SouActivity.this, "假装你已经点击了哦!", Toast.LENGTH_SHORT).show();
              }
            });
          } else if(myBase!=null) {
            sdata.clear();
            myBase.notifyDataSetChanged();
          }
        }

        @Override
        public void onFailed(Call call, IOException e) {

        }
      });
    }


  @Override
  public void afterTextChanged(Editable editable) {

  }

  class MyBase extends BaseAdapter{

    @Override
    public int getCount() {
      return sdata.size();
    }

    @Override
    public Object getItem(int i) {
      return sdata.get(i);
    }

    @Override
    public long getItemId(int i) {
      return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
      Vh vh=null;
      if(view==null){
        view=View.inflate(SouActivity.this,R.layout.item_sou,null);
        vh=new Vh();
        vh.tv1=(TextView) view.findViewById(R.id.item_sou_text1);
        view.setTag(vh);
      }else{
        vh = (Vh) view.getTag();
      }
      Log.d("main",sdata.get(i).getTitle());
      vh.tv1.setText(sdata.get(i).getTitle());
      return view;
    }
  }
  class Vh{
    TextView tv1;
  }
}

activity_sou

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_sou"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="sizu.nsg.SouActivity">
  <EditText
    android:id="@+id/app_sou"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Searching..."
    />
  <ListView
    android:id="@+id/app_sou_list"
    android:layout_below="@id/app_sou"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  </ListView>

</RelativeLayout>


item_sou

<?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">
  <TextView
    android:id="@+id/item_sou_text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="123"
    />
</RelativeLayout>

上一篇:Android自定义控件实现带文字提示的SeekBar

栏    目:Android代码

下一篇:没有了

本文标题:Android实现简单实用的搜索框

本文地址:http://www.codeinn.net/misctech/210612.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有