一、高级控件与常用控件的区别:是否使用适配器
二、适配器
1、种类
①、数组适配器 ArrayAdapter
new ArrayAdapter<String>(this,R.layout.actv_style, names);
②、简单适配器 SimpleAdapter
也叫“并不简单的简单适配器”
③、自定义适配器
2、作用:桥梁、展示
三、高级控件使用步骤:①获取数据 、②创建适配器 、③绑定适配器
四、自动提示文本框
1、种类:
①单一提示【 AutoCompleteTextView】
②多次提示【MultiAutoCompleteTextView】
//设置多次提示时,设置分隔符方法Tokenizer t = new maTokenizer();mactv_main.setTokenizer(t);
2、实例:
activity_main_xml的布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><AutoCompleteTextViewandroid:id="@+id/act_main_act1"android:layout_width="match_parent"android:layout_height="60dp" /><Spinnerandroid:id="@+id/sp_main_sp1"android:layout_width="match_parent"android:layout_height="wrap_content"></Spinner></LinearLayout>
配置MainActivity.java方法:
package com.example.t212_a07;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;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 String[] date1;//声明一个适配器private ArrayAdapter<String> adapter1;//声明提示框空间private AutoCompleteTextView act_main_act1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);act_main_act1 = findViewById(R.id.act_main_act1);//①获取数据date1 = new String[]{"愤怒的小鸟", "汤姆猫", "落汤鸡", "牛牛", "哈巴狗", "神龙", "烤 鸭", "小象", "美人鱼", "九尾狐"};//②创建适配器adapter1 = new ArrayAdapter<>(this, R.layout.act_main_item1, date1);//③绑定适配器act_main_act1.setAdapter(adapter1);}
五、下拉框(Spinner)
1、实例:
素材(在MainActivity.java里添加) :
String[] names = { "愤怒的小鸟", "汤姆猫", "落汤鸡", "牛牛", "哈巴狗", "神龙", "烤鸭","小象", "美人鱼", "九尾狐" };int[] images = { R.drawable.bird, R.drawable.cat, R.drawable.chicken,R.drawable.cow, R.drawable.dog, R.drawable.dragon,R.drawable.duck, R.drawable.elephant, R.drawable.fish,R.drawable.fox };
activity_main_xml的布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><AutoCompleteTextViewandroid:id="@+id/act_main_act1"android:layout_width="match_parent"android:layout_height="60dp" /><Spinnerandroid:id="@+id/sp_main_sp2"android:layout_width="match_parent"android:layout_height="wrap_content"></Spinner>
配置MainActivity.java方法:
package com.example.t212_a07;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;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 List<Option> data2;//声明适配器private ArrayAdapter<Option> adapter2;//声明下拉框private Spinner sp_main_sp1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);sp_main_sp2 = findViewById(R.id.sp_main_sp2);//获取数据源data2 = loadData2();//创建适配器adapter2 = new ArrayAdapter<Option>(this, R.layout.act_main_item1, data2);//绑定适配器sp_main_sp1.setAdapter(adapter2);sp_main_sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {//选中的项Option selectedItem = (Option) parent.getSelectedItem();//打印 Toast.makeText(MainActivity.this,selectedItem.getHtml(),Toast.LENGTH_LONG).show();}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}});//获取数据源的过程(下拉框)private List<Option> loadData2() {List<Book > list = new BookDAO().list();List<Option> optionList = new ArrayList<>();for (Book book : list){optionList.add(new Option(book.getId()+"",book.getName()));}return optionList;}}
六、简单适配器的实例:
activity_main_xml的布局:
简单适配器的activity_main_xml参考下拉框的activity_main_xml
配置MainActivity.java方法:
package com.example.t212_a07;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;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 List<Map<String,Object>> data3;private SimpleAdapter adapter3;private Spinner sp_main_sp2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);data3 = loadData3();adapter3 = new SimpleAdapter(this,data3,R.layout.sp_main_itme2,new String[] {"image","name"},new int[]{R.id.iv_main_item_iv1,R.id.tv_main_item_tv1});sp_main_sp2.setAdapter(adapter3);}private List<Map<String,Object>> loadData3() {List<Book> list = new BookDAO().list();List<Map<String,Object>> mapList = new ArrayList<>();Map<String,Object> map = null;for (Book book : list){map = new HashMap<>();map.put("name",book.getName());map.put("image",book.getImage());mapList.add(map);//调试// Log.i("test","----------"+map.get("name"));}return mapList;}}
七、事件监听
AdapterView.OnItemSelectedListener
如果觉得《Android的高级控件(自动提示文本框与下拉列表)》对你有帮助,请点赞、收藏,并留下你的观点哦!