zoukankan      html  css  js  c++  java
  • CheckBox控件实现选项的选中

    1:设置控件属性

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
        <!-- 注意checkbox选中之后还可以进行取消 -->
        <CheckBox
            android:checked="false"
            android:id="@+id/checkBox3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="迪迪" />
    
    </RelativeLayout>

    2:找到这个控件,设置监听器,用内部类检测监听结果并执行相应内容

    package com.Thinker.checkbox;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.OnCheckedChangeListener;
    
    public class MainActivity extends Activity {
        private CheckBox checkbox;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            /**
             * 找到相应控件对象赋给这个引用
             */
            checkbox = (CheckBox) findViewById(R.id.checkBox3);
            /**
             * 设置监听事件
             */
            checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                
                @Override
                public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                    // TODO Auto-generated method stub
                    if(arg1){
                        /*
                         * 安卓支持System.out.println来打印文字,同时也有自己的打印方法,Log.i("tag", text);
                               也可以通过日志来打印,其中tag是一个标志,text是String类型的对象
                         */
                        String text = checkbox.getText().toString();
                        Log.i("tag", text);
                    }
                }
            });
        }
        
    }
  • 相关阅读:
    微信扫码
    vue h5公众号支付
    vue h5支付宝支付
    vue PDF预览
    vue 中AES加密
    vue 动态路由配置
    移动端调试工具
    Ajax工作原理
    yahoo军规
    Flex 布局教程
  • 原文地址:https://www.cnblogs.com/rain-1/p/5148767.html
Copyright © 2011-2022 走看看