Andoir 学习总结 微件 :spinner
来源:互联网 发布:高中理科辅导软件 编辑:程序博客网 时间:2024/04/29 11:31
Andoir 微件 :spinner
sdk位置:
android-sdk-windows-1.5_r1/android-sdk-windows-1.5_r1/docs/guide/tutorials/views/hello-spinner.html
1:类:
↳
android.widget.Spinner
2:介绍
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.
3:构造函数
Spinner(Context context)
Spinner(Context context, AttributeSet attrs)
Spinner(Context context, AttributeSet attrs, int defStyle)
4:重要属性
android:prompt
The prompt to display when the spinner's dialog is shown.
5:重要函数
CharSequence
getPrompt() 返回选择的内容
void
setPrompt(CharSequence prompt)
Sets the prompt to display when the dialog is shown.
void
setPromptId(int promptId)
Sets the prompt to display when the dialog is shown.
6:数据
用Adapter 类型的数据填充条目
关于 Adapter接口
An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set
他直接的实现子类
Known Indirect Subclasses
ArrayAdapter<T>, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ListAdapter, ResourceCursorAdapter
用到比较多的是 android.widget.ArrayAdapter<T>
介绍 :A ListAdapter that manages a ListView 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. However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list. To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want
加上颜色的要注意:如果要使我们的组件变的美观的话,可以用够着函数 加上引用自TextView的布局资源,当然了 ,也可以 重载toString()方法,来决定要展示的条目 。比如ImageViews 组件。
关于ArrayAdapter 的构造方法
Public Constructors
ArrayAdapter(Context context, int textViewResourceId)
Constructor textViewResourceId: The resource ID for a layout file containing a TextView to use when instantiating views 这里要注意当时我搞错了 呵呵 ,比如可以写一个 系统里面默认的 android.R.layout.simple_spinner_item 或者别的 或者 自定义, 反正就是现实下拉的样式的
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
方法
void
setDropDownViewResource(int resource)
Sets the layout resource to create the drop down views.
1 // 设置下拉框控件的标题文本
2 spinner.setPrompt("请选择");
int pos = spinner.getSelectedItemPosition();
7: 例子
1:给控件添加数据----编程方式
我的代码:
public class spinnerDemo extends Activity {
private static String [] names=new String[]{"小勇","android","谷歌","百度"};
private ArrayAdapter adapter=null;
private ArrayList aList=null;
@SuppressWarnings("unchecked")
public void onCreate(Bundle savedInstaceState){
super.onCreate(savedInstaceState);
setContentView(R.layout.spinner);
//用自定义的数据在程序里面填充
Spinner spinner1=(Spinner)findViewById(R.id.Spinner01);
adapter=new ArrayAdapter<Object>(this,android.R.layout.simple_spinner_dropdown_item,names);
// adapter.add(R.string.items1);
// adapter.add(R.string.items1);
spinner1.setAdapter(adapter);
//用在XML文件定义的睡觉觉天从数据
Spinner spinner2=(Spinner)findViewById(R.id.Spinner02);
}
上面加上黄色底纹的 ,可以有也可以去掉,也就是说编程方式的数据来源可以是一个数组,也可是一个 XML文件里面的 (不可以同时有 ),这里要注意 ArrayAdaper的构造函数。
android.R.layout.simple_spinner_dropdown_item
也可以为别的 比如:
android.R.layout.simple_spinner_dropdown_item
只是一个每一项样式的展现,也可以自己定义 的 。
下拉样式的展现可以通过设置 spinner的 setDropDownViewResource 方法设置
运行效果
另外的一种风格
总之。没有Item 和 总体的 布局 风格自己可以改变的。当然也可以自定义的
2:在XML文件中定义的
//用在XML文件定义的睡觉觉天从数据
Spinner spinner2=(Spinner)findViewById(R.id.Spinner02);
ArrayAdapter spinner2Adapter=ArrayAdapter.createFromResource(
this, R.array.items, android.R.layout.simple_list_item_single_choice);
spinner2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(spinner2Adapter);
XML文件
名称: items.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="items">
<item>信阳</item>
<item>驻马店</item>
<item>南阳</item>
<item>北京</item>
<item>郑州</item>
<item>漯河</item>
<item>云南</item>
<item>缅甸</item>
</string-array>
</resources>
8 关键事件
代码:
// setOnItemSelectedListener() 响应下拉框的选中值发生变化的事件
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
textView.setText("你选择的是:
"+((TextView)arg1).getText()+" The position of the view in the adapter. is"+arg2+
"The row id of the item that was clicked. is "+arg3);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
textView.setText("你还没有选择任何东西呢!");
}
});
An AdapterView is a view whose children are determined by an Adapter.
public abstract voidonItemClick (AdapterView<?> parent, View view, int position, long id)
Since: API Level 1
Callback method to be invoked when an item in this AdapterView has been clicked.
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
Parameters
parent
The AdapterView where the click happened.
view
The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position
The position of the view in the adapter.
id
The row id of the item that was clicked.
可以在上面看出 为何要 使用
textView.setText(((TextView)arg1).getText());
得到选取的 文字 了 。呵呵 其他的参数 有时候 也很有用的。
我感觉 可以不经得到 选择的文字 还可 得到选择的 项的 编号 ,这个 我感觉 非常有用的 。
- Andoir 学习总结 微件 :spinner
- Android学习个人总结Spinner控件
- Spinner学习
- android spinner 总结
- spinner 的用法总结
- 单件模式学习总结(创建型)
- 【Android学习笔记】Spinner
- android:spinner学习记录
- 学习Spinner使用方法
- android学习---Spinner
- android学习---Spinner控件
- Android学习笔记 - Spinner
- Spinner组件学习笔记
- Spinner控件学习
- Spinner控件的学习
- Android学习之Spinner
- Spinner的简单学习
- Android学习之Spinner
- ACM实用C语言函数
- 第一个OpenGL程序例子
- GFW竟然开了?
- 春节流水账
- 比较好的技术类的网站网址--不断更新中
- Andoir 学习总结 微件 :spinner
- ORACLE体系结构
- 为什么要制作引导盘
- 关于BufferedImage的使用(怎样用Image来绘制图形)
- ImageObserver的用法
- 很重要的排列生成器(列出几个字符的所有排列)
- 不看这篇日志也许会节省你十分钟,但是却会耽误你的一辈子
- 由htc的ui想到的
- hdu 1026 Ignatius and the Princess I