Android基础-CheckBox用法

CheckBox与前面的RadioButton相反它是一个多选按钮这里给大家边写了一个按钮希望大家可以喜欢。

我们还是说一说它的一个实现步骤:

1、在我们的布局文件中定义好我们的布局

2、在Activity中获取我们布局好的控件

3.设置单击事件的

4、通过ishecked()这么一个方法来判断我们CheckBook是否选中选中的话就想我们定义好的字符串中添加数据

5、最后进行一个显示


-----------------------------------------------------------------------------

布局文件代码:

<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" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你为什么要学习Android "/>
   <CheckBox 
       android:id="@+id/cb1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="工资高"/>
   <CheckBox 
       android:id="@+id/cb2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="喜欢3g开发"/>
   <CheckBox 
       android:id="@+id/cb3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="父母要求学的"/>



   <Button
       android:id="@+id/bt"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="提交" />


     </LinearLayout>
</RelativeLayout>

----------------------------------------------------------------------

Activity中的代码:

package com.example.checkbox;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;


public class MainActivity extends Activity {
//定义一个字符串用来保存数据
private String s;
    private CheckBox cb1,cb2,cb3;
    private Button bt;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1=(CheckBox) findViewById(R.id.cb1);
cb2=(CheckBox) findViewById(R.id.cb2);
cb3=(CheckBox) findViewById(R.id.cb3);
bt=(Button) findViewById(R.id.bt);
//设置按钮的单击事件
bt.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//通过isChecked()方法进行判断保存数据到s中
if (!cb1.isChecked()&&!cb2.isChecked()&&!cb3.isChecked()) {
s+="请至少现在一项!";
}
if (cb1.isChecked()) {
s+=cb1.getText()+"\n";
}
if (cb2.isChecked()) {
s+=cb2.getText()+"\n";
}
if (cb3.isChecked()) {
s+=cb3.getText()+"\n";
}
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
s="";
}

});
}
}

心得:

其实大家不要看着代码好像很多的样子其实关键的代码就只有红色的那么一点,最后s="";是对保存的一个数据进行清空的,这样我们下次点击的时候就不会连同上次的数据一起显示。



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值