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="";是对保存的一个数据进行清空的,这样我们下次点击的时候就不会连同上次的数据一起显示。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章