CheckBox和Spinner控件的组合使用

本文介绍了如何在Android中组合使用CheckBox和Spinner控件。CheckBox用于复选多个选项,而Spinner则提供弹出式列表进行单一选择,两者结合可以创建更丰富的用户输入界面。文章通过一个实例展示了如何在XML布局文件中定义这两个控件,并编写相关事件监听器,包括设置Spinner的适配器和回调方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CheckBox是复选框控件 提供给用户一次选择多个选项的信息输入

使用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) {
                
            }
        });
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值