我同意条款(CheckBox的isChecked属性)

本文介绍如何在Android应用中创建一个自定义的CheckBox和Button组件,通过CheckBox的`isChecked`属性来控制Button的可点击状态,实现用户交互与功能激活的逻辑。

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

我同意条款(CheckBox的isChecked属性)

新建一个继承Activity类的CheckBoxisCheckedActivity,并设置布局文件为:checkboxischecked.xml。

在布局文件定义2个组件,CheckBox和Button

 

<CheckBox

        android:id="@+id/checkboxischecked_checkbox"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center_horizontal"

        android:text="@string/agree" />

 

 

    <Button

        android:id="@+id/checkboxischecked_btn01"

        style="?android:attr/buttonStyleInset"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/register" />

在Activity代码中获取这两个组件,单CheckBox选中的时候,Button才可以点击。

package lyx.feng.second;

......

public class CheckBoxisCheckedActivity extends Activity {

    private Button btn = null;

    private CheckBox checkBox = null;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       super.setContentView(R.layout.checkboxischecked);

       this.btn = (Button) super.findViewById(R.id.checkboxischecked_btn01);

       this.checkBox = (CheckBox) super

              .findViewById(R.id.checkboxischecked_checkbox);

       // 设置Button不可点击

       this.btn.setClickable(false);

       // 设置CheckBox默认不选中

       this.checkBox.setFocusable(false);

       this.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

 

           @Override

           public void onCheckedChanged(CompoundButton buttonView,

                  boolean isChecked) {

              if (isChecked == true) {

                  btn.setClickable(true);

              } else {

                  btn.setClickable(false);

              }

           }

       });

       this.btn.setOnClickListener(new OnClickListener() {

 

           @Override

           public void onClick(View v) {

              Toast.makeText(CheckBoxisCheckedActivity.this,

                     "You click the button.", Toast.LENGTH_SHORT).show();

           }

       });

    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值