zoukankan      html  css  js  c++  java
  • 【转】Pro Android学习笔记(十五):用户界面和控制(3):Button控件

    目录(?)[-]

    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开发相关文章

  • 相关阅读:
    设计模式学习之工厂方法(Factory Method,创建型模式)(2)
    设计模式学习之简单工厂(Simple Factory,创建型模式)(1)
    JAVA基础学习之 Map集合、集合框架工具类Collections,Arrays、可变参数、List和Set集合框架什么时候使用等(4)
    JS图片延迟加载分析及简单的demo
    SVN服务器搭建和使用(三)(转载)
    SVN服务器搭建和使用(二)(转载)
    SVN服务器搭建和使用(一)(转载)
    JAVA基础学习之String、StringBuffer、StringBuilder、基本数据类型的使用、整形进制转换、集合Collection、Vector、ArrayList、LinkedList、HashSet、TreeSet等(3)
    Entity FrameWork 中使用Expression<Func<T,true>>访问数据库性能优化
    JAVA基础学习之throws和throw的区别、Java中的四种权限、多线程的使用等(2)
  • 原文地址:https://www.cnblogs.com/blongfree/p/5047890.html
Copyright © 2011-2022 走看看