Android中的常用控件之Spinner DatePicker AutoCompleteTextView
本文由发表于6年前 | Android | 暂无评论 |  被围观 8,057 views+

Spinner


什么是Spinner?

public class
Spinner
extends AbsSpinner
implements DialogInterface.OnClickListener

A view that displays one child at a time and lets the user pick among them. The items in the Spinner come from the Adapter associated with this view.


创建一个Spinner的方法:

① 在布局文件中添加Spinner:

<Spinner
	android:id="@+id/spinnerId"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	/>

② 在strings.xml文件中添加一个string-array:

<string-array name="planets_array">
	<item>Mercury</item>
	<item>Venus</item>
	<item>Earth</item>
	<item>Mars</item>
	<item>Jupiter</item>
	<item>Saturn</item>
	<item>Uranus</item>
	<item>Neptune</item>
</string-array>

③ 在Activity中创建一个ArrayAdapter:

Spinner spinner = (Spinner) findViewById(R.id.spinnerId);
//createFromResource的第三个参数为设置Spinner的样式,使用系统自带的
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.planets_array,
				android.R.layout.simple_spinner_item);
//设置Spinner的下拉样式,也使用了系统自带的样式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setPrompt("Test");
spinner.setOnItemSelectedListener(new SpinnerOnSelectedListener());
public class
ArrayAdapter
extends BaseAdapter
implements Filterable

A concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.

④ 为Spinner编写onItemSelectedListener()对应的事件:

class SpinnerOnSelectedListener implements OnItemSelectedListener{

	@Override
	public void onItemSelected(AdapterView<?> adapterView, View view, int position,
			long id) {
	System.out.println(adapterView.getItemAtPosition(position).toString(););
	}

	@Override
	public void onNothingSelected(AdapterView<?> adapterView) {
		System.out.println("onNothingSelected()");
	}

}


除此之外,我们也可以使用List创建一个动态添加内容的Spinner:

Spinner spinner = (Spinner) findViewById(R.id.spinnerId);
List<String> list = new ArrayList<String>();
list.add("item1");
list.add("item2");
ArrayAdapter adapter = new ArrayAdapter(this,R.layout.item,R.id.textViewId,list);
spinner.setAdapter(adapter);
spinner.setPrompt("Test");
spinner.setOnItemSelectedListener(new SpinnerOnSelectedListener());
DatePicker和DatePickerDialog

DatePicker和DatePickerDialog是Android中的日期空间,用于设置日期并进行相应的操作。

public class
DatePicker
extends FrameLayout

This class is a widget for selecting a date. The date can be selected by a year, month, and day spinners or a CalendarView. The set of spinners and the calendar view are automatically synchronized. The client can customize whether only the spinners, or only the calendar view, or both to be displayed. Also the minimal and maximal date from which dates to be selected can be customized.

public class
DatePickerDialog
extends AlertDialog
implements DialogInterface.OnClickListener DatePicker.OnDateChangedListener

A simple dialog containing an DatePicker.


编写一个DatePickerDialog的方法:

① 使用匿名内部类实现DatePickerDialog.OnDateSetListener监听器:

DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {

	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		System.out.println("Date: " + year + "/" + monthOfYear + "/" + dayOfMonth);
	}
};

② 覆盖Activity中的onCreateDialog()方法:

protected Dialog onCreateDialog(int id) {
	switch (id) {
	case 1:
		return new DatePickerDialog(this, onDateSetListener, 2011, 06, 13);
	}
	return null;
}

③ 在合适的地方调用showDialog(1)方法
注意,这个日期控件的月份是从0开始的:monthOfYear 从0~11

AutoCompleteTextView
public class
AutoCompleteTextView
extends EditText
implements Filter.FilterListener

An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.


编写AutoCompleteTextView的方法:

① 在Activity的布局文件中添加AutoCompleteTextView:

<AutoCompleteTextView android:id="@+id/autocompleteId"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"/>

② 新建一个布局文件list1用于设置自动完成控件的提示框样式:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="8dp"
    android:textSize="14sp"
    android:textColor="#000">
</TextView>

③ 在Activity中获取AutoCompleteTextView并使用ArrayAdapter设置AutoCompleteTextView:

AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.autocompleteId);
//创建一个list,为ArrayAdapter提供数据
List<String> list = new ArrayList<String>();
list.add("arthinking");
list.add("answer");
list.add("awk");
list.add("android");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.list1,list);
autoCompleteTextView.setAdapter(arrayAdapter);
也可以往ArrayAdapter中传入数组,其构造函数包括以下几个:
Public Constructors
ArrayAdapter(Context context, int textViewResourceId)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId)
Constructor
ArrayAdapter(Context context, int textViewResourceId, T[] objects)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
Constructor
ArrayAdapter(Context context, int textViewResourceId, List<T> objects)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)
Constructor

(特别说明:本文部分内容是在观看marschen的Android视频教程时做的笔记,感谢marschen推出的视频教程,这里也推荐给大家:http://www.marschen.com/portal.php)

除了文章中有特别说明,均为IT宅原创文章,转载请以链接形式注明出处。
本文链接:http://www.itzhai.com/android-are-commonly-used-controls-of-the-spinner-datepicker-autocompletetextview.html
arthinking Java技术交流群:280755654,入门群:428693174 more
分享到:
 
2011 7/13
文章评论
    没有评论
给我留言

有人回复时邮件通知我
Android的相关文章
随机文章 本月热门 热评
1 Java Web笔记 – 客户端Javascript与服务器端Servlet的验证 2011/11/10
2 C++语法笔记 – 异常处理 2011/9/4
3 Tips:ItzDesktop使用技巧 2012/6/10
4 dubbo序列化方式与版本升级的问题 2016/10/15
5 Java源码分析 – LinkedList双向链表源码分析 2011/10/11
6 JPA2.0遇到的问题:NoSuchMethodError… getValidationMode()Ljavax/persistence/ValidationMode; 2014/5/13
友情推荐 更多
破博客 文官洗碗安天下,武将打怪定乾坤。多么美好的年代,思之令人泪落。
Mr.5's Life 白天是一名程序员,晚上就是个有抱负的探索者
行知-追寻技术之美 关注大数据,分布式系统
我爱编程 编程成长轨迹
Cynthia's Blog 学习笔记 知识总结 思考感悟
 
欢迎关注我的公众号 IT宅
关于IT宅 文章归档

IT宅中的文章除了标题注明转载或有特别说明的文章,均为IT宅的技术知识总结,学习笔记或随笔。如果喜欢,请使用文章下面提供的分享组件。转载请注明出处并加入文章的原链接。 感谢大家的支持。

联系我们:admin@itzhai.com

Theme by arthinking. Copyright © 2011-2015 IT宅.com 保留所有权利.