您的位置:首页 > 移动开发 > Android开发

CheckBox和Spinner控件的组合使用

2013-07-31 20:01 441 查看
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) {

                

            }

        });

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息