android获取spinner的值_android Spinner和数值选择器使用demo | 学步园
关键部分代码如下1、Spinnner在布局文件中:android:layout_width="match_parent"android:layout_height="wrap_content" >android:id="@+id/sp_select_leave_type"android:layout_width="match_parent"android:layout_height="wra
关键部分代码如下
1、Spinnner
在布局文件中:
android:layout_width="match_parent"
android:layout_height="wrap_content" >
android:id="@+id/sp_select_leave_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="85dp" />
定义对象:
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 adapter=new ArrayAdapter(this, R.layout.spinner_item, mcontentDepartment);
mSpinnerSelectDepartment.setAdapter(adapter);
}
mcontentDepartment=getApplicationContext().getResources().getStringArray(R.array.leave_department);这段代码是获取res/values中的strings中获取对应的数据:
事假
婚假
病假
每个数据显示的布局:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#f00"
android:textSize="15sp"
android:padding="10dp"
>
运行结果:
2、数值选择器
更多推荐
所有评论(0)