android搜索框功能实现

<p>这里实现的是搜索框时时查询,代码中是在数据库中使用的模糊查询;</p><p>整个布局使用的是线性布局,搜索框又是一个线性布局(里面包含一个相对布局和一个TextView,相对布局里面有一个EditText和ImageVIew),下面是一个ListView;</p><p>搜索框其实就是一个EditText,背景是用shape自己画出来的;上面放一个&quot;删除&quot;的图片;<br/></p><p>在Activity中给EditText设置一个监听,当输入文字的时候获取输入的内容然后查询数据库,将查询到的数据展示到ListView中;<br/></p><p>线来看下布局:<br/></p><p><img src="/up_pic/201808/310511173716.gif" title="310511173716.gif" alt="1.gif"/></p><pre class="brush:html;toolbar:false">&lt;LinearLayoutxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;match_parent&quot; android:orientation=&quot;vertical&quot;&gt; &lt;LinearLayout android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;43dp&quot; android:background=&quot;@color/colorPrimaryDark&quot; android:gravity=&quot;center&quot; android:orientation=&quot;horizontal&quot;&gt; &lt;RelativeLayout android:layout_width=&quot;0dp&quot; android:layout_height=&quot;match_parent&quot; android:layout_weight=&quot;1&quot;&gt; &lt;EditText android:id=&quot;@+id/edittext&quot; android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;match_parent&quot; android:layout_marginBottom=&quot;8dp&quot; android:layout_marginLeft=&quot;10dp&quot; android:layout_marginTop=&quot;8dp&quot; android:background=&quot;@drawable/searchbox_bj&quot; android:hint=&quot;请输入搜索内容&quot; android:maxLength=&quot;10&quot; android:paddingLeft=&quot;15dp&quot; android:singleLine=&quot;true&quot; android:textSize=&quot;12sp&quot;/&gt; &lt;ImageView android:id=&quot;@+id/imageview&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:layout_alignParentRight=&quot;true&quot; android:layout_centerInParent=&quot;true&quot; android:paddingRight=&quot;20dp&quot; android:src=&quot;@drawable/delete&quot; android:visibility=&quot;gone&quot;/&gt; &lt;/RelativeLayout&gt; &lt;TextView android:id=&quot;@+id/textview&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;match_parent&quot; android:gravity=&quot;center&quot; android:paddingLeft=&quot;10dp&quot; android:paddingRight=&quot;10dp&quot; android:text=&quot;搜索&quot; android:textColor=&quot;#ffffff&quot; android:textSize=&quot;20sp&quot;/&gt; &lt;/LinearLayout&gt; &lt;ListView android:id=&quot;@+id/listview&quot; android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;0dp&quot; android:layout_weight=&quot;1&quot;&gt;&lt;/ListView&gt; &lt;/LinearLayout&gt;</pre><p>CharSequence s, int start, int before, int count(CharSequence s, int start, int before, int count)方法中做判断,s是EditText中的文本内容;</p><p>判断如果s长度为0隐藏&quot;删除&quot;图片,否则显示&quot;删除图片&quot;,显示ListView,查询数据库获得Cursor获得CursorAdapter将内容展示到ListView中;</p><pre class="brush:java;toolbar:false">publicclassMainActivityextendsActivity{ privateEditTextmEditText; privateImageViewmImageView; privateListViewmListView; privateTextViewmTextView; Contextcontext; Cursorcursor; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context=this; initView(); } privatevoidinitView(){ mTextView=(TextView)findViewById(R.id.textview); mEditText=(EditText)findViewById(R.id.edittext); mImageView=(ImageView)findViewById(R.id.imageview); mListView=(ListView)findViewById(R.id.listview); //设置删除图片的点击事件 mImageView.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ //把EditText内容设置为空 mEditText.setText(&quot;&quot;); //把ListView隐藏 mListView.setVisibility(View.GONE); } }); //EditText添加监听 mEditText.addTextChangedListener(newTextWatcher(){ publicvoidbeforeTextChanged(CharSequences,intstart,intcount,intafter){}//文本改变之前执行 @Override //文本改变的时候执行 publicvoidonTextChanged(CharSequences,intstart,intbefore,intcount){ //如果长度为0 if(s.length()==0){ //隐藏&quot;删除&quot;图片 mImageView.setVisibility(View.GONE); }else{//长度不为0 //显示&quot;删除图片&quot; mImageView.setVisibility(View.VISIBLE); //显示ListView showListView(); } } publicvoidafterTextChanged(Editables){}//文本改变之后执行 }); mTextView.setOnClickListener(newView.OnClickListener(){ publicvoidonClick(Viewv){ //如果输入框内容为空,提示请输入搜索内容 if(TextUtils.isEmpty(mEditText.getText().toString().trim())){ ToastUtils.showToast(context,&quot;请输入您要搜索的内容&quot;); }else{ //判断cursor是否为空 if(cursor!=null){ intcolumnCount=cursor.getCount(); if(columnCount==0){ ToastUtils.showToast(context,&quot;对不起,没有你要搜索的内容&quot;); } } } } }); } privatevoidshowListView(){ mListView.setVisibility(View.VISIBLE); //获得输入的内容 Stringstr=mEditText.getText().toString().trim(); //获取数据库对象 MyOpenHelpermyOpenHelper=newMyOpenHelper(getApplicationContext()); SQLiteDatabasedb=myOpenHelper.getReadableDatabase(); //得到cursor cursor=db.rawQuery(&quot;select*fromlolwherenamelike&#39;%&quot;+str+&quot;%&#39;&quot;,null); MyListViewCursorAdapteradapter=newMyListViewCursorAdapter(context,cursor); mListView.setAdapter(adapter); mListView.setOnItemClickListener(newAdapterView.OnItemClickListener(){ @Override publicvoidonItemClick(AdapterView&lt;?&gt;parent,Viewview,intposition,longid){ //把cursor移动到指定行 cursor.moveToPosition(position); Stringname=cursor.getString(cursor.getColumnIndex(&quot;name&quot;)); ToastUtils.showToast(context,name); } }); } }</pre>
RangeTime:0.010083s
RangeMem:211.55 KB
返回顶部 留言