一.Spinner的简介与创建
1.Spinner的简介
Spinner(下拉列表)是一个每次只能选择所有项中一项的控件,相当于Html中的下拉列表框。它的继承关系如下:
java.lang.Object
↳ android.view.View
↳ android.view.ViewGroup
↳ android.widget.AdapterView<T extends android.widget.Adapter>
↳ android.widget.AbsSpinner
↳ android.widget.Spinner
Spinner常用的XML属性:
属性名称 |
描述 |
android:prompt |
该提示在下拉列表对话框显示时显示。(译者注:对话框的标题:
|
2.创建使用Spinner的步骤如下:
①需要在布局中定时Spinner组件,然后向Spinner添加需要选择的数据
②设置事件监听器setOnItemSelectedListener( )并实现onItemSelected( )
3.Spinner添加数据的两种方法:
①在Java代码中载入列表数据
具体的例子如下:
MainActivity.java
- package com.android.spinner;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Spinner;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- private static final String[] province={"广东","广西","湖南","河南","福建"};
- private TextView text;
- private Spinner spinner;
- private ArrayAdapter<String> adapter;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- text=(TextView)findViewById(R.id.text);
- spinner=(Spinner)findViewById(R.id.spinner);
- //将Spinner里面的可选择内容通过ArrayAdapter连接起来
- adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,province);
- //设置Spinner的样式
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- //为对话框设置标题
- //也可在XMl文件中通过“android:prompt”设置
- spinner.setPrompt("你来自哪个省");
- //为Spinner设置适配器
- spinner.setAdapter(adapter);
- //添加Spinner事件监听
- spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
- @Override
- public void onItemSelected(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- // TODO Auto-generated method stub
- text.setText("你所在的城市是:"+province[arg2]);
- //设置显示当前选择的项
- arg0.setVisibility(View.VISIBLE);
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- }
- });
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:padding="10dip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <TextView
- android:id="@+id/text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dip"
- android:text="@string/planet_prompt"
- />
- <Spinner
- android:id="@+id/spinner"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="planet_prompt">你来自的省份是:</string>
- <string name="app_name">测试Spinner</string>
- </resources>
效果图:
②在XML文件中定义列表的数据
具体的例子如下:
MainActivity.java
- package com.android.spinner;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemSelectedListener;
- import android.widget.ArrayAdapter;
- import android.widget.Spinner;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Spinner spinner = (Spinner) findViewById(R.id.spinner);
- ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
- this, R.array.province,
- android.R.layout.simple_spinner_item);
- //调用setDropDownViewResource方法,以XML的方式定义下拉菜单要显示的样式
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- //为spinner设置适配器
- spinner.setAdapter(adapter);
- spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
- }
- public class MyOnItemSelectedListener implements OnItemSelectedListener {
- public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) {
- Toast.makeText(parent.getContext(), "你来自的省份是: " +
- parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
- }
- public void onNothingSelected(AdapterView<?> parent) {
- // TODO Auto-generated method stub
- }
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:padding="10dip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <TextView
- android:id="@+id/text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dip"
- android:text="@string/planet_prompt"
- />
- <Spinner
- android:id="@+id/spinner"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:prompt="@string/planet_prompt"
- />
- </LinearLayout>
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="planet_prompt">你来自的省份是</string>
- <string name="app_name">测试Spinner</string>
- <string-array name="province">
- <item>广东</item>
- <item>广西</item>
- <item>湖南</item>
- <item>河南</item>
- <item>福建</item>
- </string-array>
- </resources>
效果图:
二.AutoCompleteTextView简介
我们平常上网的时候经常会用到Google或百度,在输入框中输入类似”51CTO“,和51CTO相关的信息就会被列出来,供用户选择,非常方便。这种效果在Android中是用AutoCompleteTextView实现的。在AutoCompleteTextView中,主要是设置想显示资源的适配器(Adapter)。
AutoCompleteTextView有三个重要的方法clearListSelection():清除选中的列表项、dismissDropDown():如果存在关闭下拉菜单、getAdapter():获取适配器。
具体的例子如下:
MainActivity.java
- package com.android.autocomplete;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ArrayAdapter;
- import android.widget.AutoCompleteTextView;
- public class MainActivity extends Activity {
- private AutoCompleteTextView atv;
- //创建字符串数组
- private static final String[] strs = {"an", "and", "android", "abc","abcdef" };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- atv = (AutoCompleteTextView) findViewById(R.id.AutoCompleteTextView1);
- //创建适配器
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
- android.R.layout.simple_dropdown_item_1line, strs);
- //为AutoCompleteTextView设置适配器
- atv.setAdapter(adapter);
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="自动完成文本框"
- />
- <AutoCompleteTextView
- android:id="@+id/AutoCompleteTextView1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
效果图:
参考:liangruijun.blog.51cto.com/3061169/652801/
http://www.blogjava.net/crazycoding/archive/2011/07/09/353981.html