CheckBox和Spinner控件的组合使用
2013-07-31 20:01
441 查看
CheckBox是复选框控件 提供给用户一次选择多个选项的信息输入
使用CheckBox要掌握设置OnCheckedChangeListener监听器和isChecked()回调方法判断当前CheckBox的状态
Spinner控件提供弹出式列表的单一选择输入
Spinner的当前选项会被显示在Spinner控件上 这种应用在手机上非常广泛 可以节省有限的屏幕空间
下面我们就来实现一个CheckBox和Spinner的组合布局
效果图如下
![](https://oscdn.geek-share.com/Uploads/Images/Content/202008/18/d2933753395fa925fc1cfd41a088f2b2)
首先我们定义xml布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkPic"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:layout_marginLeft="50dip">
<CheckBox
android:id="@+id/checkAnimal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Animal" />
<CheckBox
android:id="@+id/checkAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Amount" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkMethod"
android:textSize="20sp"
android:layout_marginTop="50dip"/>
<Spinner
android:id="@+id/spn_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dip" />
</LinearLayout>
然后我们来写Spinner的事件
public class MainActivity extends Activity {
private Spinner spinner=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner=(Spinner) findViewById(R.id.spn_type);
String[] str={"Gallery模式浏览","GridView模式浏览"};
int viewMode=0;
//定义适配器
ArrayAdapter adapter=new ArrayAdapter(this, android.R.layout.simple_spinner_item,str);
//设置Spinner点击后的选择视图的样式布局
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//绑定适配器
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
//这里代码就不提供了 具体实现什么样子的点击效果根据需求确定
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
使用CheckBox要掌握设置OnCheckedChangeListener监听器和isChecked()回调方法判断当前CheckBox的状态
Spinner控件提供弹出式列表的单一选择输入
Spinner的当前选项会被显示在Spinner控件上 这种应用在手机上非常广泛 可以节省有限的屏幕空间
下面我们就来实现一个CheckBox和Spinner的组合布局
效果图如下
首先我们定义xml布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkPic"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:layout_marginLeft="50dip">
<CheckBox
android:id="@+id/checkAnimal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Animal" />
<CheckBox
android:id="@+id/checkAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Amount" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkMethod"
android:textSize="20sp"
android:layout_marginTop="50dip"/>
<Spinner
android:id="@+id/spn_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dip" />
</LinearLayout>
然后我们来写Spinner的事件
public class MainActivity extends Activity {
private Spinner spinner=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner=(Spinner) findViewById(R.id.spn_type);
String[] str={"Gallery模式浏览","GridView模式浏览"};
int viewMode=0;
//定义适配器
ArrayAdapter adapter=new ArrayAdapter(this, android.R.layout.simple_spinner_item,str);
//设置Spinner点击后的选择视图的样式布局
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//绑定适配器
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
//这里代码就不提供了 具体实现什么样子的点击效果根据需求确定
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
相关文章推荐
- CheckBox与其他控件的组合使用
- Android下拉框控件Spinner的使用
- 【按住你的心】——Android开发CheckBox&RadioButton控件的简单使用
- 安卓ListView和CheckBox组合使用
- ASP.NET中 CheckBox复选框控件的使用
- 由群发短信功能学习:CheckBox和ListView组合使用的方法和注意事项.
- Android开发--RadioButton和CheckBox控件的使用
- Android之下拉列表Spinner控件使用
- Gridview添加Checkbox控件的使用
- 使用jquery与图片美化checkbox和radio控件的代码(打包下载)
- Spinner下拉列表控件属性的使用细节总结
- 安卓开发_复选按钮控件(CheckBox)的简单使用
- android自学第四天 Spinner 和 AutoCompleteTextView 基本适配器控件的使用
- [原创]使用DataList分页显示数据,双向排序,添加checkbox控件。
- 使用easyui框架form控件,单选按钮radio或复选框checkbox样式问题
- CheckBox控件及OnCheckedChangeListener监听器的使用,RadioGroup及RadioButton的使用
- 在Android开发中使用自定义组合控件的例子
- MFC CHECKBOX控件的使用
- Androidのspinner控件使用
- 下拉列表(Spinner),多选框(CheckBox),单选按钮(RadioButton)的使用