目录(?)[-]

  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. JavaScript-Confirm用法

    function checkMobileBind() {    if(confirm('您尚未绑定手机,是否前往绑定?'))    {        window.location='http://m ...

  2. Intel IPP 图像空间转换

    一. 背景 用QuickSync VPP模块做RGBA到NV12的颜色空间转换导致文字显示蒙上一层颜色的问题, 暂时怀疑是VPP自身的问题,因为参数设置都是按官方demo设置的.所以尝试使用IPP来做 ...

  3. python基础18 ---多态与绑定方法

    一.抽象类 1.抽象类的定义:从一堆类中抽象出相同的内容,重新组成一个新的类,这样的类属于抽象类. 2.香蕉类是一类水果,苹果类是一类水果,葡萄类是一类水果,但是他们都属于水果,从他们这些类中可以抽象 ...

  4. python中reduce()函数

    reduce()函数也是Python内置的一个高阶函数.reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接收 ...

  5. web前端框架之自定义form表单验证

    自定义form验证初试 .在后端创建一个类MainForm,并且在类中自定义host ip port phone等,然后写入方法,在post方法中创建MainForm对象,并且把post方法中的sel ...

  6. dsp2812 pwm配置

    肚子疼了好几天,今天稍微好点,简单写点东西. 好几个月前做的项目,有些地方已经记不清楚了,但是突然客户又来问关于代码配置的情况,重新查看了代码,把相关的知识也整理一下. dsp2812中有好几个时钟相 ...

  7. Elipse 快捷键

    1. eclipse里面如何快速收缩当前类文件里面的所有方法和注释收缩:ctrl+shift+/展开:ctrl+shift+*注意:这个/和*要是数字键盘上的/和*.   2. shift+enter ...

  8. spring boot项目启动报(No session repository could be auto-configured, check your configuration (session store type is 'null'))

    找到项目的application配置文件,增加 spring.session.store-type=none,重新启动问题解决 注:因为项目未使用redis管理session,可以如上设置,如果想使用 ...

  9. UniDAC 安装教程

    翻译: 1.解压后把UniDAC文件夹直接复制到你专门用来存放第三方控件的地方(这一步根据自己的喜好,可以跳过这一步)2.在UniDAC\Source\Delphi21文件夹中找到Make.bat文件 ...

  10. Hibernate学习---第十一节:Hibernate之数据抓取策略&批量抓取

    1.hibernate 也可以通过标准的 SQL 进行查询 (1).将SQL查询写在 java 代码中 /** * 查询所有 */ @Test public void testQuery(){ // ...