AutoCompleteTextView
一.自动提示文本输入框且不区分大小写
二.适配器ArrayAdapter
1.数据源是String[ ]或集合,保存要提示的文本
2.layout文件
三.completionThreshold
1.设置提示需要的最小字符数
2.默认是2
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp2.TestActivity11" 11 android:orientation="vertical"> 12 13 <AutoCompleteTextView 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:hint="请输入..." 17 android:id="@+id/at_1" 18 android:completionThreshold="1"/> 19 </LinearLayout>
1 package com.hanqi.testapp2; 2 3 import android.app.Notification; 4 import android.app.NotificationManager; 5 import android.app.PendingIntent; 6 import android.content.Intent; 7 import android.os.Bundle; 8 import android.support.v7.app.AppCompatActivity; 9 import android.view.View; 10 import android.widget.AdapterView; 11 import android.widget.ArrayAdapter; 12 import android.widget.AutoCompleteTextView; 13 import android.widget.Spinner; 14 import android.widget.Toast; 15 16 public class TestActivity11 extends AppCompatActivity { 17 18 AutoCompleteTextView at_1; 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_test11); 23 24 at_1=(AutoCompleteTextView)findViewById(R.id.at_1); 25 //准备数据源 26 String[]strings={"abc","And","bea","car","abcde","AAAaaa","anddd"}; 27 28 //准备适配器 29 ArrayAdapter<String> aa=new ArrayAdapter<String>(this,R.layout.array_adapter,strings); 30 31 //给组件构建适配器 32 at_1.setAdapter(aa); 33 } 34 }
Spinner
一.概念:下拉列表框
二.适配器ArrayAdapter
1.数据源是String[ ]或集合,保存要提示的文本
2.layout文件
三.监听器AdapterView.OnItemSelectedListener()
1.void onItemSelected(AdapterView<?> parent, View view, int position, long id)
2.void onNothingSelected(AdapterView<?> parent)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp2.TestActivity11" 11 android:orientation="vertical"> 12 <LinearLayout 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content"> 15 <TextView 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:text="学历:"/> 19 <Spinner 20 android:layout_width="0dp" 21 android:layout_weight="1" 22 android:layout_height="wrap_content" 23 android:id="@+id/sp_1"></Spinner> 24 </LinearLayout> 25 </LinearLayout>
1 package com.hanqi.testapp2; 2 3 import android.app.Notification; 4 import android.app.NotificationManager; 5 import android.app.PendingIntent; 6 import android.content.Intent; 7 import android.os.Bundle; 8 import android.support.v7.app.AppCompatActivity; 9 import android.view.View; 10 import android.widget.AdapterView; 11 import android.widget.ArrayAdapter; 12 import android.widget.AutoCompleteTextView; 13 import android.widget.Spinner; 14 import android.widget.Toast; 15 16 public class TestActivity11 extends AppCompatActivity { 17 18 Spinner sp_1; 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_test11); 23 24 sp_1=(Spinner)findViewById(R.id.sp_1); 25 //下拉列表 26 final String[] xl={"高中","专科","本科","硕士","博士"}; 27 28 ArrayAdapter<String> sp=new ArrayAdapter<String>(this,R.layout.array_adapter,xl); 29 30 sp_1.setAdapter(sp); 31 32 //监听 33 sp_1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 34 @Override 35 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 36 37 Toast.makeText(TestActivity11.this, "选中的项目是"+xl[position], Toast.LENGTH_SHORT).show(); 38 } 39 40 @Override 41 public void onNothingSelected(AdapterView<?> parent) { 42 43 Toast.makeText(TestActivity11.this, "什么也没选", Toast.LENGTH_SHORT).show(); 44 45 } 46 }); 47 48 } 49 }