资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

单选按钮RadioGroup与复选框CheckBox-创新互联

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

成都创新互联成都网站建设定制网站制作,是成都网站制作公司,为成都纯水机提供网站建设服务,有成熟的网站定制合作流程,提供网站定制设计服务:原型图制作、网站创意设计、前端HTML5制作、后台程序开发等。成都网站改版热线:028-86922220

一、设计登录窗口

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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享标题:单选按钮RadioGroup与复选框CheckBox-创新互联
URL地址:http://cdkjz.cn/article/egoio.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220