0%
这是一片思考的空间 -- arthinking
Spring 重构&代码整洁之道 软件设计 JVM 并发编程 数据结构与算法 分布式 存储 网络 微服务 设计模式
Java技术栈 - 涉及Java技术体系

Android中的常用控件之Spinner DatePicker AutoCompleteTextView

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:

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

Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune

③ 在Activity中创建一个ArrayAdapter:

Spinner spinner = (Spinner) findViewById(R.id.spinnerId);
//createFromResource的第三个参数为设置Spinner的样式,使用系统自带的
ArrayAdapter 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 list = new ArrayList();
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:

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


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

AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.autocompleteId);
//创建一个list,为ArrayAdapter提供数据
List list = new ArrayList();
list.add("arthinking");
list.add("answer");
list.add("awk");
list.add("android");
ArrayAdapter arrayAdapter = new ArrayAdapter(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 objects) Constructor ArrayAdapter(Context context, int resource, int textViewResourceId, List objects) Constructor

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

欢迎关注我的其它发布渠道

订阅IT宅
内功修炼
Java技术栈
Java架构杂谈是IT宅精品文章公众号,欢迎订阅:
📄 网络基础知识:两万字长文50+张趣图带你领悟网络编程的内功心法 📄 HTTP发展史:三万长文50+趣图带你领悟web编程的内功心法 📄 HTTP/1.1:可扩展,可靠性,请求应答,无状态,明文传输 📄 HTTP/1.1报文详解:Method,URI,URL,消息头,消息体,状态行 📄 HTTP常用请求头大揭秘 📄 HTTPS:网络安全攻坚战 📄 HTTP/2:网络安全传输的快车道 📄 HTTP/3:让传输效率再一次起飞 📄 高性能网络编程:图解Socket核心内幕以及五大IO模型 📄 高性能网络编程:三分钟短文快速了解信号驱动式IO 📄 高性能网络编程:彻底弄懂IO复用 - IO处理杀手锏,带您深入了解select,poll,epoll 📄 高性能网络编程:异步IO:新时代的IO处理利器 📄 高性能网络编程:网络编程范式 - 高性能服务器就这么回事 📄 高性能网络编程:性能追击 - 万字长文30+图揭秘8大主流服务器程序线程模型
📄 Java内存模型:如果有人给你撕逼Java内存模型,就把这些问题甩给他 📄 一文带你彻底理解同步和锁的本质(干货) 📄 AQS与并发包中锁的通用实现 📄 ReentrantLock介绍与使用 📄 ReentrantReadWriteLock介绍与使用 📄 ReentrantLock的Condition原理解析 📄 如何优雅的中断线程 📄 如何优雅的挂起线程 📄 图解几个好玩的并发辅助工具类 📄 图解BlockingQueue阻塞队列
📄 消息队列那么多,为什么建议深入了解下RabbitMQ? 📄 高并发异步解耦利器:RocketMQ究竟强在哪里? 📄 Kafka必知必会18问:30+图带您看透Kafka
📄 洞悉MySQL底层架构:游走在缓冲与磁盘之间 📄 SQL运行内幕:从执行原理看调优的本质 📄 洞悉Redis技术内幕:缓存,数据结构,并发,集群与算法