关键部分代码如下
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() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //获得每项选中的数据 mleaveType= getApplicationContext().getResources().getStringArray(R.array.leave_type)[position]; } @Override public 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、数值选择器
时间: 2024-10-28 15:14:12