android Spinner和数值选择器使用demo
16lz
2021-01-24
关键部分代码如下
1、Spinnner
在布局文件中:
<TableRow android:layout_width="match_parent" android:layout_height="wrap_content" > <Spinner android:id="@+id/sp_select_leave_type" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="85dp" /> </TableRow>
定义对象:
private Spinner mSpinnerSelectLeaveType;//选择请假类型
获得对象后绑定监听事件:
mSpinnerSelectLeaveType.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view,int position, long id) {//获得每项选中的数据mleaveType= getApplicationContext().getResources().getStringArray(R.array.leave_type)[position];}@Overridepublic void onNothingSelected(AdapterView<?> parent) {Toast.makeText(getApplicationContext(), "确认是否正确选择", 500).show();}});
给spinnner绑定数据关键代码如下:
/**
* 为请假人部门spinner绑定数据
*/
private void setDepartmentAdapter(){
mcontentDepartment=getApplicationContext().getResources().getStringArray(R.array.leave_department);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.spinner_item, mcontentDepartment);
mSpinnerSelectDepartment.setAdapter(adapter);
}
mcontentDepartment=getApplicationContext().getResources().getStringArray(R.array.leave_department);这段代码是获取res/values中的strings中获取对应的数据:
<string-array name="leave_type"> <item >事假</item> <item >婚假</item> <item >病假</item> </string-array>
每个数据显示的布局:
<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#f00" android:textSize="15sp" android:padding="10dp" > </TextView>
运行结果:
2、数值选择器
更多相关文章
- android 不销毁当前activity,返回上一层,亲测有效
- Android(安卓)SDK 源码下载,eclipse关联代码
- android 输入对话框 确认对话框
- Android(安卓)G711A 音频编解码,去除“吱吱”电流声,附上so下载地
- Android(安卓)使用RecyclerView的方法
- Android(安卓)获取验证码倒计时实现
- [置顶] Android(安卓)IPC 通讯机制源码分析【下】
- Android(安卓)设置Dalog大小
- Android(安卓)【将图片网址Url转化为Bitmap工具类】