这篇文章主要介绍了Android如何实现搜索框,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
盘州网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。成都创新互联成立于2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联。
具体内容如下
展示效果
代码区
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 Listsdata; 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 (); 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 () { @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
item_sou
感谢你能够认真阅读完这篇文章,希望小编分享的“Android如何实现搜索框”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!