Android 按钮类控件大集锦:Button ToggleButton CheckBox RadioButton

来源:互联网 发布:成都盘古网络离职 编辑:程序博客网 时间:2024/05/19 21:01

原文地址 

http://www.cnblogs.com/mengdd/archive/2013/01/17/2865320.html

Button

  Button的基本使用之前已经讨论过:

  http://www.cnblogs.com/mengdd/archive/2012/12/20/2826235.html

  其中介绍了两种处理点击的方法:一种是通过在布局文件中设置onClick属性;另一种是通过在程序中设置Listener。

  这两种方法对于本文中其他控件也适用,只不过第二种方法可能具体函数名称会有所变化。

  对于Button的应用补充一些内容:

  Button可以有文字的、图形的、或者文字和图形都有的。

  主要是文字的,在布局文件中声明 Button 即可;图形的,就声明为ImageButton,并且添加属性src

  比如:

<ImageButton    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:src="@drawable/button_icon"    ... />

 

  如果是文字和图形都有,则使用 Button 类,但是加上android:drawableLeft 属性设置图像:

<Button    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/button_text"    android:drawableLeft="@drawable/button_icon"    ... />

Styling Your Button

  可以设置按钮的风格和主题,这部分内容先不详述,如有兴趣可以转向文末尾的参考链接。

 

无边按钮

  加上属性style="?android:attr/borderlessButtonStyle"

  如:

复制代码
<Button    android:id="@+id/button_send"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/button_send"    android:onClick="sendMessage"    style="?android:attr/borderlessButtonStyle" />
复制代码

自定义背景

  可以定义按钮各种状态对应的图片,首先需要把这些图片资源加入进来,然后在res/drawable/目录下创建一个XML文件,将状态和对应的图片绑定;最后在Button的android:background 属性中加入该XML文件的名字。

  比如:

复制代码
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/button_pressed"          android:state_pressed="true" />    <item android:drawable="@drawable/button_focused"          android:state_focused="true" />    <item android:drawable="@drawable/button_default" /></selector>
复制代码

 

  注意到这个文件中item的顺序是很重要的,当这个drawable资源被引用时,<item>元素会按顺序被查询如果匹配立即返回,不再向后查询

  所以把默认的情况放在最后,当前面的状态都不匹配时,显示默认图像。

  

复制代码
<Button    android:id="@+id/button_send"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/button_send"    android:onClick="sendMessage"    android:background="@drawable/button_custom"  />
复制代码

   

  drawable除了可以是图片以外,也可以是自己定义的shape,比如这个state_list其中的drawable就是自己定义的shape(当然从这个文件中是看不出来的,它们和图像的引用方式没有差别):

复制代码
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/shape_one" android:state_pressed="true"/>    <!-- pressed -->    <item android:drawable="@drawable/shape_two" android:state_focused="true"/>    <!-- focused -->    <item android:drawable="@drawable/shape_three"/>    <!-- default --></selector>
复制代码

  

 

  其中自定义的shape:shape_one.xml:

复制代码
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle" >    <gradient        android:angle="45"        android:endColor="#80FFBBFF"        android:startColor="#FFFFBB00" />    <padding        android:bottom="7dp"        android:left="7dp"        android:right="7dp"        android:top="7dp" />    <corners android:radius="8dp" /></shape>
复制代码

 

 shape_two.xml

 

 shape_three.xml


  具体可以参见:API Guides: Drawable Resources

  http://developer.android.com/guide/topics/resources/drawable-resource.html


自定义Button字体颜色:

  根据上面的思路,字体颜色也可以采用自定义变化的方式:

  比如设置Button的属性如下:

复制代码
    <Button        android:id="@+id/button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/myTextView"        android:background="@drawable/button_state_list"        android:text="Test Button"        android:textColor="@drawable/btn_text_color_state_list" />
复制代码

 

  其中btn_text_color_state_list.xml:

复制代码
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true" android:color="#ffff0000"/>    <!-- pressed -->    <item android:state_focused="true" android:color="#ff0000ff"/>    <!-- focused -->    <item android:color="#ff000000"/>    <!-- default --></selector>
复制代码


 

Toggle Buttons

  这种按钮ToggleButton只涉及两种状态的转换,Android 4.0 (API level 14)开始,引入了 Switch,可以进行滑动控制,如下图:

  

 

  可以更改android:textOnandroid:textOff属性,设置按钮状态为true和false时的文字。

 

 

复选框Checkbox

    复选框类 CheckBox 是CompoundButton类的子类,而CompoundButton类是Button类的子类,是带有选择和未选择状态的Button。

  关于实现,直接看代码吧。

 

单选按钮

  单选按钮用于从一个集合中选中一项。

  单选组将用户的可选项全部罗列出来,让用户选择一个,如果不需要全部罗列出来,可以考虑换用spinner,关于spinner的使用先不赘述。

  每一个单选按钮项是一个 RadioButton ,因为单选项目是互斥的,所以需要把它们结成组,组成 RadioGroup,系统会使得一组中每次只要一个被选中。

  RadioButton和CheckBox一样,都是CompoundButton的子类。

  而RadioGroup却继承自LinearLayout,默认是垂直布局。

 

  除了给每个RadioButton的onClick属性赋值以外(同一个函数名),还可以通过对RadioGroup设置事件监听。

  调用RadioGroup对象的setOnCheckedChangeListener方法。

 

  RadioButton不像CheckBox那样,RadioButton不可以通过二次点击来取消选择。

  但是可以通过在RadioGroup外部的按钮之类的,对整个RadioGroup进行清除,清除后,checkedId变为-1,表示没有单选按钮被选择。

  

  

程序实例

  还是看直接的例子比较简单有效:

  程序运行如下图:

  

 

 

  例子中:

  第一个按钮是无边框按钮;

  第二个按钮本来是一个小按钮,后来用于实践了自定义背景的效果,按钮被点击和获得焦点时的图片与默认图片不同;

  第三个按钮是一个ImageButton,通过这个按钮试了试长按和点击的事件;

  之后是两个Toggle Button,第一个是默认文字,添加了事件相应,第二个更改了两种状态的文字;

  之后是Check Box和Radio Group。

  这里利用了最初的第一个按钮,作为一个重置按钮,将多选和单选的状态都设置为没有选择,并且两个Toggle Button都设置为false。

 

代码

布局文件:

activity_hello_button.xml

Activity代码:

HelloButton.java

Button自定义图片效果需要的配置文件(放在res/drawable/目录下):

button_custom.xml

字符串资源文件:

strings.xml

 

 

参考资料

  API Guides: Buttons

  http://developer.android.com/guide/topics/ui/controls/button.html

  API Guides: Styles and Themes

  http://developer.android.com/guide/topics/ui/themes.html

  API Guides: Toggle Buttons

  http://developer.android.com/guide/topics/ui/controls/togglebutton.html

  API Guides: Checkboxes

  http://developer.android.com/guide/topics/ui/controls/checkbox.html

  API Guides: Radio Buttons

  http://developer.android.com/guide/topics/ui/controls/radiobutton.html

0 0