Android实现简单实用的搜索框
程序员文章站
2023-11-16 16:16:58
本文实例为大家分享了android实现搜索框展示的具体代码,供大家参考,具体内容如下
展示效果
代码区
souactivity
public cla...
本文实例为大家分享了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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: AI怎么绘制一大一小的篮球?