Android——实现【搜索框】与点击搜索事件监听——SearchView

it2022-05-05  241

效果图: 点击历史记录的item 2 点击历史记录的 item 3 时刻监听搜索框中的内容 点击搜索后,2 — 二界面,3 — 三界面

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="15dp" android:orientation="vertical" tools:context=".MainActivity"> <SearchView android:id="@+id/searchView" android:layout_width="match_parent" android:layout_height="wrap_content" android:iconifiedByDefault="false" android:queryHint="请输入搜索内容" /> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="265dp" /> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>

activity_main2.xml 与 activity_main3.xml 几乎一样就是二与三的区别而已

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Main2Activity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="50dp" android:text=""/> </RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity { private String info; // 得到输入框的内容 private SearchView mSearchView; // 输入框对象 private ListView listView; private List<String> mList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initList(); mSearchView = findViewById(R.id.searchView); this.listView = (ListView) findViewById(R.id.listView); // 搜索框中 ListView item的点击事件 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { String str = (String)((TextView) view).getText(); if (str.equals("1")){ Intent intent = new Intent(MainActivity.this,MainActivity.class); startActivity(intent); }else if (str.equals("2")){ Intent intent = new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); }else if (str.equals("3")){ Intent intent = new Intent(MainActivity.this,Main3Activity.class); startActivity(intent); } } }); listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mList)); listView.setTextFilterEnabled(true); // 设置搜索文本监听 mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { // 当点击搜索按钮时触发该方法 @Override public boolean onQueryTextSubmit(String query) { if (info.equals("1")){ Intent intent = new Intent(MainActivity.this,MainActivity.class); startActivity(intent); }else if (info.equals("2")){ Intent intent = new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); }else if (info.equals("3")){ Intent intent = new Intent(MainActivity.this,Main3Activity.class); startActivity(intent); } return false; } // 当搜索内容改变时触发该方法,时刻监听输入搜索框的值 @Override public boolean onQueryTextChange(String newText) { if (!TextUtils.isEmpty(newText)){ info = newText; // newText输入搜索框的值 listView.setFilterText(newText); }else{ listView.clearTextFilter(); } return false; } }); } private void initList(){ mList.add("1"); mList.add("2"); mList.add("3"); } }

M2与M3代码几乎一致,就不列举了,简介点让你们看起来比较舒服!

如果有问题,可以联系我qq:1787424177

最后,我真诚的希望能评论一句嘛,让我知道你来过,我会很开心的


最新回复(0)