网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

单选按钮RadioGroup与复选框CheckBox

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

创新互联建站-专业网站定制、快速模板网站建设、高性价比芗城网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式芗城网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖芗城地区。费用合理售后完善,10年实体公司更值得信赖。

一、设计登录窗口

打开“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,其文本修改为"保存"。

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


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


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


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


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


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


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


二、单击事件 

 打开“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。



网页标题:单选按钮RadioGroup与复选框CheckBox
文章出自:http://bjjierui.cn/article/ihhjes.html

其他资讯