目录(?)[-]

  1. 基础Button
  2. ImageButton
  3. ToggleButton
  4. CheckBox
  5. RadioButton

基础Button

Button是最常用的基础控件之一,在Android中是TextView的继承类。在上面衍生ImageButton和ToggleButton,我们将逐一介绍。小例子如图。基础Button我们将主要介绍按键触发的方式。代码如下:

Button bt = (Button)findViewById(R.id.ui_button1); 
bt.setOnClickListener(new OnClickListener() { 
    
//本例是通过内部匿名类来实现,当然也可以引用View.OnClickListener接口的对象来进行处理。 
    public void onClick(View v) { 
        //… 具体的触发处理代码  
        Log.d("UiButtonTest","Basic Button was clicked!"); 
    } 
});

Android还提供了另一种方式,在XML中通过android:onClick指定触发的方法名字,我们在Button的继承类ImageButton中给出案例。

ImageButton

图中第二排都是ImageButton。实现的XML片段如下:

<ImageButton android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:id="@+id/ui_imgbutton1"  
        android:src="@drawable/ic_launcher" <!-- 图片来源,位于res/drawable下 --> 
        android:onClick="myClickFunc"
  <!-- 指定了点击控件后的触发的方法 --> 
        android:background="@null"/>  <!-- 设置背景为null,去掉按钮形状的背景 --> 
         
    <ImageButton android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:id="@+id/ui_imgbutton2"  
        android:onClick="myClickFunc" <!-- 本例中两个按钮采用同样触发方法 --> 
        android:src="@drawable/ic_launcher"
/>  
         
    <ImageButton android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:id="@+id/ui_imgbutton3"  
        android:contentDescription="image Button" <!--  添加以下描述性语言,但不在UI上显示 --> 
        android:src="@drawable/ic_launcher" />

对于code,如下:

public void myClickFunc(View v){ //对应XML中的android:onClick 
    switch(v.getId()){  //多个控件对应同意触发,可重复利用代码,并可区分来自哪个控件的触发 
    case R.id.ui_imgbutton1: 
    case R.id.ui_imgbutton2: 
        Log.d("UiButtonTest","Image Button was clicked! Changed Image"); 
        ((ImageButton)v).setImageResource(R.drawable.sample_image);  //更改ImageButton的图片 
        break; 
    default: 
        break; 
    } 
}

ImageButton还可以进行一些有趣的变化,在不同情况下,显示不同的图案。小例子的第三排就是用于测试这种情况。

在res/drawable目下设置一个xml文件,用于描述不同情况下图片的选择,例子如下:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:state_pressed="true" android:drawable="@drawable/png03" /> 
    <item android:state_focused="true" android:drawable="@drawable/png02" /> 
    <item android:drawable="@drawable/png01" />
 
</selector>

需要注意,selector中item的顺序是有讲究的,系统将从第一条开始匹配,逐条匹配,直至成功。我们缺省显示png01图片,如果这条方在第一位置,将马上被匹配到,其他选项就不起作用。res/layout下相应的xml如下:

<ImageButton android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/ui_button_selector"/> <!--  指向在res/drawable中描述选择的xml文件 -->

ToggleButton

ToggleButton就是开关类按钮,有两个状态,开和关。xml如下:

<ToggleButton android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/ui_toggle" 
    android:textOn="Runing"  <!--  开状态的显示 --> 
    android:textOff="Stop"/>  <!--  关状态的显示 -->

缺省情况下,按钮初始为关,每按一次,按钮在开和关中切换。我们也可以在代码中指定开关状态,如下:

ToggleButton tb = (ToggleButton) findViewById(R.id.ui_toggle); 
tb.setChecked(true);

CheckBox

CheckBox和Button从用户的视角上看很不同,但是在Android中,CheckBox是android.widget.CompoundButton的继承类,而ComPoundButton是android.widget.Button的继承类。XML如下:

<CheckBox android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/ui_cb_apple" 
    android:text="@string/ui_cb_apple" 
   android:checked="true"/> 
<CheckBox android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/ui_cb_banana" 
    android:text="@string/ui_cb_banana" 
   android:checked="false"   <!-- 缺省为flase,可以不进行说明 --> 
    android:onClick="myClickFunc"/> <!—CheckBox是Button的子类,所以可以通过onClick来设置触发方法 --> 
<CheckBox android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/ui_cb_melon" 
    android:text="@string/ui_cb_melon" 
    android:checked="true" />

CheckBox有两个状态,checked和unchecked,在code中,可以通过setChecked(true|false)来设置状态,也可以通过toggle()来修改状态。应用可能会关心checkbox状态改变,代码如下:

CheckBox cbApple = (CheckBox)findViewById(R.id.ui_cb_apple); 
cbApple.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {  
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
        Log.d("UiButtonTest","CheckBox Apple state change to " + isChecked); 
    } 
});

if(cbApple.isChecked())  //检查checkbox的状态 
    cbApple.toggle();       //切换checkbox的状态

CheckBox是Button的子类,我们也可以利用Button的setOnClickListener来实现,以及在xml中定义点击触发方法,如本例的public void myClickFunc(View v)来实现。

RadioButton

RadioButton是多选一,由容器RadioGroup进行封装。在一个radiogroup中最多只能有一个选项。RadioButton是Button继承类CompoundButton的继承类,而RadioGroup是LinearLayout的继承类。RadioGroup具有线性布局的全部特性,可以包含非radioButton的控件,例如本例中,放入了一个TextView。下面是xml片段:

<RadioGroup android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/ui_rbGroup" 
    android:orientation="vertical"> 
    <TextView android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/fruit"/>  <!-- RadioGroup容器中可以放入其他控件--> 
    <RadioButton android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/ui_rb_apple" 
        android:text="@string/apple" /> 
    <RadioButton android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/ui_rb_banana" 
        android:text="@string/banana" 
        android:checked="true"/>  <!-- 缺省,RadioButton是unchecked的,可以为其中一个设置checked --> 
    <RadioButton android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/ui_rb_melon" 
        android:text="@string/melon" />" 
</RadioGroup>

在代码中,我们可以设置radiobutton的状态,已经查询那个radio button是checked,如下:

RadioGroup rbGroup =(RadioGroup)findViewById(R.id.ui_rbGroup); 
RadioButton rbApple = (RadioButton)findViewById(R.id.ui_rb_apple); 
RadioButton rbBanana = (RadioButton)findViewById(R.id.ui_rb_banana); 
//试验:设置RadioButton状态 
rbBanana.toggle(); //toggle()可以改变状态,但是在radio button中有一些不同,可以将状态从unchecked改变为checked,但不能反过来(导致全部都没有选中),本例中这句不起作用,所以toggle()需要慎用 
rbBanana.setChecked(false); //可以用setChecked()设定状态,本例运行到此句,全部为unchecked状态 
rbGroup.clearCheck();           //也可以通过group.clearCheck() 进行清除,全部为unchecked 
rbApple.toggle();                   //本例运行到此句,选定Apple。

//测试:通过代码方式,在Group中新增一个RadioButton 
RadioButton rbCherry = new RadioButton(this); 
rbCherry.setText("Cherry"); 
rbCherry.setId(CHERRY_ID);  //如果不指定,缺省从1开始分配,但根据编程原则,我们应自行设定 
rbGroup.addView(rbCherry);

//测试:获取选择的ID 
int checkId = rbGroup.getCheckedRadioButtonId(); //将返回R.id.ui_rb_apple

当用户改变选项,可以通过注册触发来进行处理,如下:

//【注意】建议指明是RadioGroup,例如程序中原来已经对checkbox等控件进行处理,编译会认为是CompoundButton类,因此如果程序涉及多种控件,应给出更为精确的描述 
rbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) {  
        Log.d("UiButtonTest","Changed checked ID to " + checkedId); 
        switch(checkedId){ 
        case -1: //-1 为全部是unchecked情况 
            Log.d("UiButtonTest","Choices cleared."); 
            break; 
        default: 
            RadioButton rb = (RadioButton)group.findViewById(checkedId); 
            Log.d("UiButtonTest","Chose " + rb.getText()); 
            break; 
        } 
    } 
});

相关链接: 我的Android开发相关文章

【转】Pro Android学习笔记(十五):用户界面和控制(3):Button控件的更多相关文章

  1. 【转】 Pro Android学习笔记(五二):ActionBar(5):list模式

    可以在action bar中加入spinner的下来菜单,有关spinner,可以参考Pro Android学习笔记(二十):用户界面和控制(8):GridView和Spinner. list的样式和 ...

  2. 【转】 Pro Android学习笔记(五六):配置变化

    目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...

  3. 【转】 Pro Android学习笔记(五十):ActionBar(3):搜索条

    目录(?)[-] ActionBar中的搜索条 通过Menu item上定义search view 进行Searchable的配置 在activity中将search view关联searchable ...

  4. 【转】 Pro Android学习笔记(五七):Preferences(1):ListPreference

    目录(?)[-] 例子1ListPreference小例子 定义一个preferences XML文件 继承PreferenceActivity 用户定制偏好的读取 第一次运行时设置缺省值 设置Cat ...

  5. 【转】 Pro Android学习笔记(五九):Preferences(3):EditText和Ringtone Preference

    目录(?)[-] EditText Preferences xml文件 设备的存贮文件 Ringtone Preferences EditText Preferences xml文件 在res/xml ...

  6. 【转】 Pro Android学习笔记(五八):Preferences(2):CheckBoxPreference

    目录(?)[-] CheckBox Preference xml文件 设备的存贮文件 复合preference 在ListPreference的例子中显示的是单选,如果是多选,可采用CheckBoxP ...

  7. 【转】Pro Android学习笔记(五):了解Content Provider(上)

    Content Provider是抽象数据封装和数据访问机制,例如SQLite是Android设备带有的数据源,可以封装到一个content provider中.要通过content provider ...

  8. 【转】Pro Android学习笔记(五三):调试和分析(1):Debug视图和DDMS视图

    目录(?)[-] Debug视图 DDMS视图 查看应用运行状态 进入debug状态 HPROF Thread信息 Method信息 Stop 截图 UI层次架构信息 其它的 Tab中提供的功能 我们 ...

  9. 【转】 Pro Android学习笔记(五五):调试和分析(3):adb命令、模拟器控制台和StrictMode

    目录(?)[-] adb命令 模拟器Console StrictMode adb命令 我们在学习SQLite的使用,介绍过部分adb命令的使用,见Pro Android学习笔记(五):了解Conten ...

  10. 【转】Pro Android学习笔记(二五):用户界面和控制(13):LinearLayout和TableLayout

    目录(?)[-] 布局Layout 线性布局LinearLayout 表格布局TableLayout 布局Layout Layout是容器,用于对所包含的view进行布局.layout是view的子类 ...

随机推荐

  1. 我的Android进阶之旅------>Android中android:visibility 属性VISIBLE、INVISIBLE、GONE的区别

    在Android开发中,大部分控件都有visibility这个属性,其属性有3个分别为"visible "."invisible"."gone&quo ...

  2. 磁盘分区对齐详解与配置 – Linux篇

    在之前一篇<磁盘分区对齐详解与配置 – Windows篇>中,我介绍了磁盘分区对齐的作用和适用于MBR和GPT的两种磁盘类型的配置,以及Windows平台设置磁盘分区对齐的方法. 本文作为 ...

  3. STM32L0 HAL库 IO读写功能

    开发环境使用 MDK5.16a + CUBEMX生成代码 开发板使用:NUCLEO-L053R8 核心芯片:STM32L053R8 今天主要学习了下最基础的IO的读写,IO使用 PA5   LED输出 ...

  4. QCon2016 上海会议汇总(1) - 前端技术实践

    日程传送门:http://2016.qconshanghai.com/schedule 我这里重点总结下前端.移动端.团队管理.研发支撑相关的议题,谈谈我的感受. <Vue 2.0: 渐进式前端 ...

  5. Python 3 面向对象 一

    Python 3 面向对象 一.面向过程-->面向对象 面向过程:根据业务逻辑从上到下堆叠代码,即先干什么再干什么,基于面向过程去设计程序就好比在设计一条流水线,是一种机械式的思维方式 函数式: ...

  6. C++的动态库和静态库(dll)

    一,在VS里面 新建项目->Visual C++ -> win32 控制台应用程序 -> 填写项目名称->下一步选择 dll : 二,自动生成的文件如图: 以项目名称生成的My ...

  7. EntityFramework 学习 一 Explicit Loading with DBContext

    即使延迟加载不能使用,也可以通过明确的调用来延迟加载相关实体 使用DBEntryEntity来完成 using (var context = new SchoolDBEntities()) { //D ...

  8. tomcat7 中的坑。 关于welcome-list和servlet-mapping

    web.xml中, 使用default servlet设置了针对静态资源后缀名的过滤. 并且设置了welcome-list, 使用jetty和tomcat6启动一切正常, 但是使用tomcat7则出现 ...

  9. java的远程访问接口的实例

    被别人远程调用和自己远程调用别人接口的理解: 被别人调用接口:其实没有什么神秘的,就是我们写一个正常的方法提供一个访问路径. 调用别人的接口:本质时一个Request请求,实际要使用到javax.ne ...

  10. 解决Eclipse和myeclipse在进行 html,jsp等 页面编辑时,自动格式化变丑的问题

    在eclipse和myelipse写JAVA代码时中使用ctrl+shift+f 快捷键自动排版省时又省力,排版后的代码规范美观又层次性,但在我们写jsp或html代码时,使用这个快捷键排版简直奇丑无 ...