CheckBox在控件 两种方式实现

这篇博客介绍了在Android中使用CheckBox实现选择功能的两种方法。通过设置OnCheckedChangeListener,当CheckBox的状态改变时,显示相应的Toast提示。第一种方法是直接将Activity实现OnCheckedChangeListener接口并重写方法;第二种方法是在每个CheckBox上设置新的匿名内部类实例。
摘要由CSDN通过智能技术生成

布局代码详情:(下配效果图)

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="22sp"
        android:text="请选择喜欢的应用程序:" />
    <CheckBox 
        android:id="@+id/cd_wx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="微信"
        />
     <CheckBox 
        android:id="@+id/cd_wy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="微云"
        />
      <CheckBox 
        android:id="@+id/cd_wb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="微博"
        />


</LinearLayout>

第一种方法:

package com.ayxy.arrj_empathy;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity implements OnCheckedChangeListener{
//接口  implements   OnCheckedChangeListener 需要重写方法
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //1.导入布局
        setContentView(R.layout.activity_main);
        //2.创建对象
         CheckBox cd_wx = (CheckBox) findViewById(R.id.cd_wx);//此处需要强制类型转换
         CheckBox cd_wy = (CheckBox) findViewById(R.id.cd_wy);
         CheckBox cd_wb = (CheckBox) findViewById(R.id.cd_wb);
        //3.指挥对象做事情
         cd_wx.setOnCheckedChangeListener(this);//CheckBox 使用的第一种办法 
         cd_wy.setOnCheckedChangeListener(this);
         cd_wb.setOnCheckedChangeListener(this);
    }
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// 4.buttonView 事件源  isChecked 是否被选中
switch (buttonView.getId()) {
case R.id.cd_wx:
Toast.makeText(MainActivity.this, isChecked?"以选择:微信":"已取消", 0).show();
break;
case R.id.cd_wy:
Toast.makeText(MainActivity.this, isChecked?"以选择:微云":"已取消", 0).show();
break;
case R.id.cd_wb:
Toast.makeText(MainActivity.this, isChecked?"以选择:微博":"已取消", 0).show();
break;


default:
break;
}
}

}

第二种方法:

package com.ayxy.arrj_empathy;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;


public class MainActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //1.导入布局
        setContentView(R.layout.activity_main);
        //2.创建对象
         CheckBox cd_wx = (CheckBox) findViewById(R.id.cd_wx);//此处需要强制类型转换
         CheckBox cd_wy = (CheckBox) findViewById(R.id.cd_wy);
         CheckBox cd_wb = (CheckBox) findViewById(R.id.cd_wb);
        //3.指挥对象做事情
         cd_wx.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//CheckBox 使用的第二种办法 
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// buttonView 事件源   isChecked 判断是否被选中
if(isChecked)
{Toast.makeText(MainActivity.this, "以选择微信", 0).show();}
else {Toast.makeText(MainActivity.this, "以取消", 0).show();}
}
});
         cd_wy.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  //CheckBox 使用的第二种办法 
  @Override
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  // buttonView 事件源   isChecked 判断是否被选中
  if(isChecked)
  {Toast.makeText(MainActivity.this, "以选择微云", 0).show();}
  else {Toast.makeText(MainActivity.this, "以取消", 0).show();}
  }
  });
         cd_wb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  //CheckBox 使用的第二种办法 
  @Override
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  // buttonView 事件源   isChecked 判断是否被选中
  if(isChecked)
  {Toast.makeText(MainActivity.this, "以选择微博", 0).show();}
  else {Toast.makeText(MainActivity.this, "以取消", 0).show();}
  }
  });
    }

}


奥的斯

电池的撒

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值