zoukankan      html  css  js  c++  java
  • 了解Android_06之CheckBox

    一、CheckBox是个什么东西?

      CheckBox即复选框。

    二、CheckBox样式:

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="你学习的编程语言:"
            android:textSize="24sp"
        />
        <CheckBox
            android:id="@+id/checkbox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="PHP"
            android:textSize="22sp"
            android:layout_marginTop="10dp"
            android:button="@drawable/checkbox"
            android:paddingLeft="10dp"
        />
        <CheckBox
            android:id="@+id/checkbox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Java"
            android:textSize="22sp"
            android:layout_marginTop="10dp"
            android:paddingLeft="10dp"
            android:button="@drawable/checkbox"
        />

    分析:

     自定义样式xml:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:drawable="@drawable/checkbox_true"/>
        <item android:state_checked="false" android:drawable="@drawable/checkbox_false"/>
    </selector>

    自定义样式引用了两张图片:

    未选中:

     被选中:

     真机调试效果为:勾选后出现被选中的图片,没勾选则使用未选中的图片

    三、复选框监听事件:

    public class MainActivity extends AppCompatActivity {
        private CheckBox checkBox1,checkBox2;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            checkBox1 = findViewById(R.id.checkbox1);
            checkBox2 = findViewById(R.id.checkbox2);
            checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    Toast.makeText(MainActivity.this,b?"checkBox1被选中":"checkBox1未被选中",Toast.LENGTH_SHORT).show();
                }
            });
            checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    Toast.makeText(MainActivity.this,b?"checkBox2被选中":"checkBox2未被选中",Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    由于监听事件比较易懂,这里就不做过多的注释了。

  • 相关阅读:
    你试过不用if撸代码吗?
    Chrome开发者工具Debug入门
    我为什么推荐Prettier来统一代码风格
    使用JSDoc自动生成代码文档
    Async/Await是这样简化JavaScript代码的
    C#泛型约束 (转载)
    DateTime , DateTime2 ,DateTimeOffset 之间的小区别 (转载)
    允许跨域资源共享(CORS)携带 Cookie (转载)
    C#中如何利用操作符重载和转换操作符 (转载)
    EF Core 2.1 Raw SQL Queries (转自MSDN)
  • 原文地址:https://www.cnblogs.com/wmskywm/p/13866607.html
Copyright © 2011-2022 走看看