作者微信 bishe2022

代码功能演示视频在页面下方,请先观看;如需定制开发,联系页面右侧客服
Android开发之路——单选框,复选框,弹出框等控件操作

Custom Tab

由于这几个控件都是比较常用的控件,所以在进行操作的时候会比较常用,所以这个部分算是Android软件开发的重要部分,内容比较简单。分类型进行介绍

1.单选框操作:单选框在Android里面随处可见,它是由两部分组成的,一部分是RadioGroup,一部分是RadioButton。一个RadioGroup里面是有多个RadioButton。每个RadioButton就是一个单选项,而控制的时候是控制RadioGroup。下面是Xml和代码的实现部分:

xml:

<RadioGroup  
        android:id = "@+id/genderGroup"  
        android:layout_width = "wrap_content"  
        android:layout_height = "wrap_content"  
        android:orientation = "horizontal"  
        >  
          
        <RadioButton  
            android:id = "@+id/femaleButton"  
            android:layout_width = "wrap_content"  
            android:layout_height = "wrap_content"  
            android:text = "@string/female"/>  
  
        <RadioButton  
            android:id = "@+id/maleButton"  
            android:layout_width = "wrap_content"  
            android:layout_height = "wrap_content"  
            android:text = "@string/male"/>  
    </RadioGroup>

上面定义了一个简单的RadioGroup和RadioButton的显示。

下面是绑定这个控件的事件的操作代码:

//通过绑定genderGroup的OnCheckedChangeListener的方法来进行事件响应   
       genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
          
        @Override  
        public void onCheckedChanged(RadioGroup group, int checkedId) {  
            if(femaleButton.getId() ==checkedId){  
                System.out.println("female");  
                Toast.makeText(Activity07.this, "女", Toast.LENGTH_SHORT).show();  
            }else if(maleButton.getId() == checkedId){  
                System.out.println("male");  
                Toast.makeText(Activity07.this, "男", Toast.LENGTH_SHORT).show();  
            }  
        }  
    });

2.弹出框(Toast)弹出框的事件跟Swing的JOptionPane很像,但是它是叫Toast,使用的方法就是在你需要弹出信息的地方加上:Toast.makeText(这里是你要弹出的类对象名,这个是你要弹出的字符串 , 这个是你要弹出的长度大小)。具体例子看上面一段Java代码的最后一行。弹出框不需要在xml里面进行配置。

3.复选框(checkBox):复选框就没有单选框那样有组的概念了,所以复选框的操作和单选框比起来就会比较复杂一点点,因为你要对每个复选框都进行一个事件响应。下面是一个复选框的例子。

<CheckBox  
        android:id = "@+id/swim"  
        android:layout_width = "wrap_content"  
        android:layout_height = "wrap_content"  
        android:text = "@string/swim"/>  
      
    <CheckBox  
        android:id = "@+id/run"  
        android:layout_width = "wrap_content"  
        android:layout_height = "wrap_content"  
        android:text = "@string/run"/>  
      
    <CheckBox  
        android:id = "@+id/read"  
        android:layout_width = "wrap_content"  
        android:layout_height = "wrap_content"  
        android:text = "@string/read"/>

转载自:http://www.linuxidc.com/Linux/2011-08/40651.htm

Android开发之路——走进Android(工程结构剖析) 

http://wisdomdd.cn/Wisdom/resource/articleDetail.htm?resourceId=556

Android开发之路——第一个Android小程序(Android电话拨号器)

http://wisdomdd.cn/Wisdom/resource/articleDetail.htm?resourceId=557

Android开发之路——第二个Android小程序(Android短信发送)

http://wisdomdd.cn/Wisdom/resource/articleDetail.htm?resourceId=558

Android开发之路——第三个Android小程序(Android的Activity显示

http://wisdomdd.cn/Wisdom/resource/articleDetail.htm?resourceId=559

Android开发之路——Android的布局初步 

http://wisdomdd.cn/Wisdom/resource/articleDetail.htm?resourceId=560

Android开发之路——Android的布局初步2——TableLayout布局 

http://wisdomdd.cn/Wisdom/resource/articleDetAail.htm?resourceId=561

Android开发学习笔记补充记录——Activity的生命周期 

http://wisdomdd.cn/Wisdom/resource/articleDetail.htm?resourceId=563


Home