Android實(shí)現(xiàn)簡(jiǎn)單實(shí)用的搜索框
本文實(shí)例為大家分享了Android實(shí)現(xiàn)搜索框展示的具體代碼,供大家參考,具體內(nèi)容如下
展示效果

代碼區(qū)
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, "假裝你已經(jīng)點(diǎn)擊了哦!", 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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android SearchView搜索框組件的使用方法
- Android搜索框通用版
- Android搜索框組件SearchView的基本使用方法
- Android 根據(jù)EditText搜索框ListView動(dòng)態(tài)顯示數(shù)據(jù)
- android搜索框上下滑動(dòng)變色效果
- Android EditText搜索框?qū)崿F(xiàn)圖標(biāo)居中
- Android的搜索框架實(shí)例詳解
- Android中如何實(shí)現(xiàn)清空搜索框的文字
- Android搜索框SearchView屬性和用法詳解
- Android搜索框(SearchView)的功能和用法詳解
相關(guān)文章
Android集成GreenDao數(shù)據(jù)庫(kù)的操作步驟
這篇文章主要介紹了Android集成GreenDao數(shù)據(jù)庫(kù),使用數(shù)據(jù)庫(kù)存儲(chǔ)時(shí)候,一般都會(huì)使用一些第三方ORM框架,比如GreenDao,本文分幾步給大家介紹Android集成GreenDao數(shù)據(jù)庫(kù)的方法,需要的朋友可以參考下2022-10-10
Android實(shí)現(xiàn)水波紋擴(kuò)散效果的實(shí)例代碼
這篇文章主要介紹了Android實(shí)現(xiàn)水波紋擴(kuò)散效果的實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法
今天小編就為大家分享一篇Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Android編程使用自定義shape實(shí)現(xiàn)shadow陰影效果的方法
這篇文章主要介紹了Android編程使用自定義shape實(shí)現(xiàn)shadow陰影效果的方法,涉及Android中xml文件布局的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android 判斷ip地址合法實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 判斷ip地址合法實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-06-06
android?studio實(shí)現(xiàn)上傳圖片到j(luò)ava服務(wù)器
這篇文章主要為大家詳細(xì)介紹了android?studio實(shí)現(xiàn)上傳圖片到j(luò)ava服務(wù)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Android使用Jni實(shí)現(xiàn)壓力鍋數(shù)據(jù)檢測(cè)效果示例
這篇文章主要介紹了Android使用Jni實(shí)現(xiàn)壓力鍋數(shù)據(jù)檢測(cè)效果,涉及Android結(jié)合Jni實(shí)現(xiàn)進(jìn)度條模擬壓力鍋數(shù)據(jù)監(jiān)測(cè)效果的相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
Android實(shí)現(xiàn)簡(jiǎn)單的下拉刷新pulltorefresh
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單的下拉刷新pulltorefresh的相關(guān)代碼,具有一定的實(shí)用性和操作價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07

