1.AutoCompleteText自动完成文本框(自动补全)
首先让我们来看一下我们要完成的效果图(右)
具体代码实现
布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="com.zking.android07_widgetplus.MainActivity">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/actv_main_auto"
android:completionThreshold="1"/><!--当输入1个字符串时会有提示-->
</LinearLayout>
strings.xml
<resources>
<string name="app_name">Android07_WidgetPlus</string>
<!--字符串数组-->
<string-array name="data">
<item>张三</item>
<item>张三丰</item>
<item>李四</item>
<item>李四他爹</item>
<item>王麻子</item>
<item>王晓晓</item>
<item>admin</item>
<item>豆豆</item>
<item>安安</item>
</string-array>
</resources>
MainActivity.java
package com.zking.android07_widgetplus;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
private AutoCompleteTextView actv_main_auto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actv_main_auto = (AutoCompleteTextView) findViewById(R.id.actv_main_auto);
//获取字符串数组
String data[]=getResources().getStringArray(R.array.data);//拿到res文件夹下面的strings.xml
//适配器
ArrayAdapter adapter=new ArrayAdapter(this,R.layout.item_actv,data);//Ctrl+P 第二个:每一行的排版效果(每一行的布局文件)
//给控件设置适配器
actv_main_auto.setAdapter(adapter);
}
}
item_actv.xml
(自动完成文本框的每一行效果)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tv_item_actv_text"
android:textSize="30sp"
>
</TextView>
2.MultiAutoCompleteTextView(有空格搜索,用的不多,简单作为了解)
效果图如下
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="com.zking.android07_widgetplus.MainActivity"
android:orientation="vertical"
>
<!--多个自动完成文本框-->
<MultiAutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mactv_main_auto"
android:completionThreshold="1"
/>
</LinearLayout>
MainActivity.java
package com.zking.android07_widgetplus;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
private MultiAutoCompleteTextView mactv_main_auto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mactv_main_auto = (MultiAutoCompleteTextView) findViewById(R.id.mactv_main_auto);
//一个适配器一个数据源可以供多个控件使用
//获取字符串数组
String data[]=getResources().getStringArray(R.array.data);//拿到res文件夹下面的strings.xml
//适配器
ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,data);//Ctrl+P 自己的:R.layout.item_actv 系统的:android.R.layout.(让下拉框变好看)
//给控件设置分隔符
MultiAutoCompleteTextView.Tokenizer tokenizer=new MultiAutoCompleteTextView.CommaTokenizer();
mactv_main_auto.setTokenizer(tokenizer);
//给控件设置适配器
mactv_main_auto.setAdapter(adapter);
}
}
3.Spinner+SimpleAdapter
效果图:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="com.zking.android07_widgetplus.MainActivity"
android:orientation="vertical"
>
<!--下拉列表-->
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/s_main_spinner"
>
</Spinner>
</LinearLayout>
MainActivity.java
package com.zking.android07_widgetplus;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private Spinner s_main_spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s_main_spinner = (Spinner) findViewById(R.id.s_main_spinner);
//给下拉列表设置适配器
final String provinces[]={"湖南省","河南省","海南省"};//省份
int images[]={R.drawable.dog,R.drawable.elephant,R.drawable.fox};//图片{R.drawable.图片名}
//循环省份(Map)
List<Map<String,Object>> list=new ArrayList<>();
for (int i = 0; i < provinces.length; i++) {//provinces.fori+回车
Map<String,Object> map=new HashMap<>();
map.put("title",provinces[i]);//("键能随便写",值不能随便写)
map.put("image",images[i]);
list.add(map);
}
//第一种:适配器(ArrayAdapter只能放一组(文字),如果再放一组图片就满足不了要求了,所以要升级)
//ArrayAdapter adapterSpinner=new ArrayAdapter(this,android.R.layout.simple_list_item_1,provinces);//系统自带的:android.R.layout.
//第二种:升级SimpleAdapter
SimpleAdapter adapterSpinner=new SimpleAdapter(this,list,android.R.layout.activity_list_item,new String[]{"title","image"},new int[]{android.R.id.text1,android.R.id.icon});//查看括号里面放什么:Ctrl+P resource:每一行展示的效果
s_main_spinner.setAdapter(adapterSpinner);
//给下列列表设置选择事件
s_main_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,provinces[position],Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
4.ListView(一行一行效果,使用率非常广泛 ,记得配置xml)
效果图:
activity_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv_list_names"
>
</ListView>
</LinearLayout>
ListActivity.java
package com.zking.android07_widgetplus;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2017/6/8 0008.
*/
public class ListActivity extends AppCompatActivity{
private ListView lv_list_names;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
lv_list_names = (ListView) findViewById(R.id.lv_list_names);
//数据源
final List<String> names=new ArrayList<>();
for (int i = 0; i <100 ; i++) {
names.add("张"+i);
}
//适配器
ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,names);
lv_list_names.setAdapter(adapter);
//给ListView设置点击事件
lv_list_names.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ListActivity.this,names.get(position),Toast.LENGTH_SHORT).show();
}
});
}
}