单选按钮RadioGroup与复选框CheckBox

AndroidApp应用中,单选按钮和复选框也是经常使用的,下面我们一起学习一下。我们需要学习Android中的基本控件:(1)单选按钮RadioGroup(2)复选框CheckBox

一、设计登录窗口

  打开“res/layout/activity_main.xml”文件。

   1、分别从工具栏向activity拖出1个单选按钮列表RadioGroup(注意自动包含3个单选按钮RadioButton)、2个复选框CheckBox1个按钮Button。这3个控件均来自Form Widgets

2、打开activity_main.xml文件。

  我们把自动生成的代码修改成如下代码,具体为:

  (1RatioGroupid修改为gender,两个RadioButtonid分别修改为malefemale,其文本分别修改为男和女;

  注意:第1个单选按钮android:checked="true"表示此单选按钮默认为选择。

  (2)两个CheckBoxid修改为footballbasketball,其文本分别修改为足球和蓝球;

  (3Buttionid修改为save,其文本修改为"保存"

<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" >


    <RadioGroup
        android:id="@+id/gender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >


        <RadioButton
            android:id="@+id/male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/male" />


        <RadioButton
            android:id="@+id/female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/female" />
    </RadioGroup>


    <CheckBox
        android:id="@+id/football"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/gender"
        android:layout_below="@+id/gender"
        android:text="@string/football" />


    <CheckBox
        android:id="@+id/basketball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/football"
        android:layout_below="@+id/football"
        android:text="@string/basketball" />


    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/basketball"
        android:layout_below="@+id/basketball"
        android:layout_marginTop="28dp"
        android:text="@string/save" />


</RelativeLayout>

二、单击事件 

  打开“src/com.genwoxue.RadioGroupCheckBox/MainActivity.java文件。

  然后输入以下代码:

package com.example.hw;


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

import android.widget.RadioButton;
import android.widget.Toast;


public class MainActivity extends Activity {
private RadioButton rbMale = null;
private RadioButton rbFemale = null;
private CheckBox cbFootBall = null;
private CheckBox cbBasketBall = null;
private Button btnSave = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rbMale = (RadioButton) super.findViewById(R.id.male);
rbFemale = (RadioButton) super.findViewById(R.id.female);
cbFootBall = (CheckBox) super.findViewById(R.id.football);
cbBasketBall = (CheckBox) super.findViewById(R.id.basketball);
btnSave = (Button) super.findViewById(R.id.save);//刚开始括号里的Button写成CheckBox了,导致ClassCastException

      //而且模拟器出现Unfortunately,project名has stopped错误
btnSave.setOnClickListener(new SaveOnClickListener());

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class SaveOnClickListener implements OnClickListener{

public void onClick(View v) {
String sGender = "";
String sFav = "";
String sInfo = "";
if(rbMale.isChecked()){
sGender = rbMale.getText().toString();
}
if(rbFemale.isChecked()){
sGender = rbFemale.getText().toString();
}
if(cbFootBall.isChecked()){
sFav = sFav+cbFootBall.getText().toString();
}
if(cbBasketBall.isChecked()){
sFav = sFav+cbBasketBall.getText().toString();
}
   sInfo = "性别:"+sGender+"    爱好:"+sFav;
Toast.makeText(getApplicationContext(), sInfo, Toast.LENGTH_SHORT).show();

}


}
}

在以上代码中,我们着重分析一下带有绿色部分,其它是最简单的基础代码,如果不明白,请参考上一章内容。

  1、第部分

  导入与RadioButtonCheckBox相关的2个包。

  2、第部分

  声明5个控件变量。

  3、第部分

  与上一章类同。

  (1findViewById()方法完成5个控件的捕获。

  (2保存按钮添加单击监听事件:btnSave.setOnClickListener(new SaveOnClickListener())

  4、第部分

  我们新建一个类SaveOnClickListener继承接口OnClickListener用以实现单击事件监听。

  Toast.makeText(getApplicationContext(), sInfo,Toast.LENGTH_SHORT).show()用以显示提示信息:性别与爱好。

  注意:isChecked()方法用来判断RadioButtonCheckBox控件是否被选中,如果选中返回true,否则返回flase


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
框和单选按钮是常见的用户界面控件,用于允许用户择一个或多个项。在使用这些控件时,通常需要处理控件的事件来获取用户的择。 对于框,通常使用isChecked()方法来获取它的状态,如果返回true则表示它被中,否则为未中。在处理框的事件时,可以使用setOnCheckedChangeListener()方法来设置一个监听器,当框的状态发生改变时,该监听器会被调用。 示例代码: ``` CheckBox checkBox = findViewById(R.id.checkbox); checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { // 处理中状态 } else { // 处理未中状态 } } }); ``` 对于单选按钮,通常使用RadioGroup来管理一组单选按钮,并使用getCheckedRadioButtonId()方法来获取当前中的单选按钮的ID。在处理单选按钮的事件时,可以使用setOnCheckedChangeListener()方法来设置一个监听器,当中的单选按钮发生改变时,该监听器会被调用。 示例代码: ``` RadioGroup radioGroup = findViewById(R.id.radio_group); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.radio_button1: // 处理单选按钮1的情况 break; case R.id.radio_button2: // 处理单选按钮2的情况 break; // 可以添加更多的项 } } }); ``` 需要注意的是,以上示例代码中的R.id.checkbox、R.id.radio_group、R.id.radio_button1、R.id.radio_button2等都是控件的ID,需要在布局文件中定义并分配给相应的控件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值