温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

单选按钮RadioGroup与复选框CheckBox

发布时间:2020-07-04 06:41:19 来源:网络 阅读:1053 作者:没有水勒鱼 栏目:移动开发

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


一、设计登录窗口

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

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

2、打开activity_main.xml文件。

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

  (1)RatioGroup的id修改为gender,两个RadioButton的id分别修改为male和female,其文本分别修改为男和女;

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

  (2)两个CheckBox的id修改为football和basketball,其文本分别修改为足球和蓝球;

  (3)Buttion的id修改为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、第①部分

  导入与RadioButton、CheckBox相关的2个包。

  2、第②部分

  声明5个控件变量。

  3、第③部分

  与上一章类同。

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

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

  4、第④部分

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

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

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




向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI