android Spinner(列表选择框)
来源:互联网 发布:混沌骑士 知乎 编辑:程序博客网 时间:2024/05/17 04:26
在res目录下编写列表选择框中内容,点击界面中所选择的item时,会在eclipse的logcat中显示所选内容,点击button时,toast显示选择内容。共三个文件
1.spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:text="请选择证件类型"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Spinner
android:entries="@array/ctype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner1"
/>
<Button
android:text="提交"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
2.arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ctype">
<item>身份证</item>
<item>学生证</item>
<item>军人证</item>
<item>工作证</item>
<item>其他</item>
</string-array>
</resources>
3.MainActivity
setContentView(R.layout.spinner);
final Spinner spinner = (Spinner) findViewById(R.id.spinner1);
// spinner.getSelectedItem();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String result = parent.getItemAtPosition(position).toString();
Log.i("Tag", result);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,
spinner.getSelectedItem().toString(), Toast.LENGTH_LONG)
.show();
}
});
也可以采用适配的方式
setContentView(R.layout.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(MainActivity.this,
R.array.ctype, android.R.layout.simple_dropdown_item_1line);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
Log.i("Tag", spinner.getSelectedItem().toString());
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,
spinner.getSelectedItem().toString(), Toast.LENGTH_SHORT)
.show();
}
});
- android Spinner(列表选择框)
- 列表选择框(Spinner)
- Android练习-----列表选择框(Spinner) (上)
- Android练习-----列表选择框(Spinner) (下)
- Spinner:列表选择框
- 列表选择框spinner
- 列表选择框spinner
- 列表选择框Spinner
- Spinner(列表选择框)的使用
- Android学习笔记三--列表选择框(Spinner)
- Android如何愉快的使用Spinner列表选择框
- Android开发系列(二十一):Spinner的功能和用法以及实现列表选择框
- Android 下拉列表框(spinner)
- 高级控件之列表选择框(Spinner)
- Android--下拉列表框(Spinner)
- android 下拉列表(Spinner)
- android 下拉选择框Spinner
- android 下拉选择框Spinner
- 李开复给中国大学生的第一封信—从诚信谈起
- 伺服电机编码器与转子磁极相位对齐方法
- 【cocos2d-x 3.5】Lua Action相关
- Linux下Tomcat使用80端口
- 学生信息管理系统之总结篇
- android Spinner(列表选择框)
- static的用法
- Oracle 单行函数
- Android核心基础-5.Android 数据存储与访问-4.ContentProvider 内容提供者
- 推荐!国外程序员整理的机器学习资源大全
- VF控制和矢量控制的一些区别
- android 下改变默认的checkbox的 选中 和被选中 图片
- Oracle检查与安装操作内容
- JDK中Applet的安全性策略探究