目录
searchView
searchView是搜索框.提供搜索框的图形界面.
使用方式:
<SearchView
android:id="@+id/searchview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp" />
image.png
searchView具有以下属性:
image.png
实战
activity_main.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<SearchView
android:id="@+id/searchview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp" />
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
</ListView>
</android.support.constraint.ConstraintLayout>
代码:
package com.example.user.searchview;
import android.os.Trace;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
public class MainActivity extends AppCompatActivity {
ListView lv;
SearchView sv;
String[] name= {"111", "222", "333"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.listview);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, name));
lv.setTextFilterEnabled(true);
sv = (SearchView)findViewById(R.id.searchview);
//是否自动缩小为图标
sv.setIconifiedByDefault(false);
//现实默认文字
sv.setQueryHint("查找");
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if(!TextUtils.isEmpty(newText)){
lv.setFilterText(newText);
}else{
lv.clearTextFilter();
}
return true;
}
});
}
}
执行效果:
image.png image.png
网友评论