Android中Spinner的使用方法
来源:互联网 发布:html调用js函数 编辑:程序博客网 时间:2024/05/23 18:56
(1)在布局文件中的声明
public void onNothingSelected(AdapterView parent) {
<Spinner
android:id="@+id/spinnerld"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
(2)在String.xml当中声明一个数组
<String-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
</String-array>
(3)创建一个ArrayAdapter
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
创建下拉菜单的样式
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
定义soinner中每个条目的样式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
设置spinner对象(通过设置赋值给spinner对象)
spinner.setAdapter(adapter);
setPrompt("设置下拉菜单的标题")
}
(3)创建一个监听器
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext()), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
(4)在Oncreat方法中注册监听器
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
ArrayAdapter的另一种用法
除了从可以使用strings.xml文件当中的数组创建ArrayAdapter之外,还可以动态的创建 ArrayAdapter
List<String>list = new ArrayList<String>();
list.add("test1");
lsit.add("test2“);
ArrayAdapter adapter = new ArrayAdapter(this,R.layout.item,R.id.textViewld,list);
0 0
- Android中Spinner的使用方法
- android中Spinner的使用方法
- android spinner的使用方法
- Xamarin android spinner的使用方法
- (Spinner) android中Spinner的使用
- android下拉菜单spinner的使用方法
- android下拉菜单spinner的使用方法
- Android学习 - 下拉菜单spinner的使用方法
- android下拉菜单spinner的使用方法
- android下拉菜单spinner的使用方法
- android下拉菜单spinner的使用方法
- android下拉菜单spinner的使用方法
- android下拉菜单spinner的使用方法
- android下拉菜单spinner的使用方法
- android下拉菜单spinner的使用方法
- ANDROID下拉菜单SPINNER的使用方法
- Android Spinner控件使用方法
- Android中Spinner的使用
- python 中各种编码关系
- 上班语录 2014-03-07
- 设计模式C++实现(2)——策略模式
- 创业公司和求职者都应看的九个面试题
- emacs下cedet的配置
- Android中Spinner的使用方法
- 品牌的成长离不开服务质量的提高
- 信息增益(IG,Information Gain)的理解和计算
- 14随谈
- 设计模式C++实现(3)——适配器模式
- 产品经理的自我修养:方法论
- Matlab虚拟现实工具箱——快速配置
- Lua 笔记 1-1 基本函数使用
- mssql查看数据库中所有触发器语句