2.2 按钮类控件的使用

一、Button组件的功能与用法

Button、CheckBox、RadioButton、RadioGroup 、ToggleButton 、Switch、ImageButton、CompoundButton:这些都属于按钮组件,这里重点介绍下 Button 的用法。

Button 继承了 TextView,所以 TextView 上很多属性也可以应用到 Button 上,他主要是在UI界面上生成一个按钮,可供用户单击。当用户单击按钮时,按钮会触发一个 onClick 事件,我们可以在这里处理自己想要的逻辑。

下面的例子实现一个功能更强大的按钮:

  • 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- 文字带阴影的按钮 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文字带阴影的按钮"
        android:textSize="12pt"
        android:shadowColor="#aa5"
        android:shadowRadius="1"
        android:shadowDx="5"
        android:shadowDy="5"/>
    <!-- 普通文字按钮 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/red"
        android:text="普通按钮"
        android:textSize="10pt"/>
    <!-- 带文字的图片按钮-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_selector"
        android:textSize="18px"
        android:text="带文字的图片按钮"/>
</LinearLayout>

上面的布局中第一个是普通按钮,但是为该按钮指定了阴影(与TextView的配置方式相同);第二个按钮通过background配置了背景图片,因为图片是圆形的,所以在这里显示出圆形的样子;第三个按钮给背景指定了选择器,引用的Drawable资源对应如下:

<?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/red" />
    <!-- 指定按钮松开时的图片 -->
    <item android:state_pressed="false"
    android:drawable="@drawable/purple" />
</selector>

我们实际开发中对于Button的,无非是对按钮的几个状态做相应的操作,比如:按钮按下的时候用一种颜色,弹起又一种颜色,或者按钮不可用的时候一种颜色这样!所以说这种方法我们会经常用到,以此指定松开和按下时的不同效果。

StateListDrawable 是 Drawable 资源的一种,可以根据不同的状态,设置不同的图片效果,关键节点 < selector >,我们只需要将 Button 的 background 属性设置为该drawable资源即可轻松实现,按下按钮时不同的按钮颜色或背景!我们可以设置的属性有:

常用属性:

drawable:引用的Drawable位图,我们可以把他放到最前面,就表示组件的正常状态
state_focused:是否获得焦点
state_window_focused:是否获得窗口焦点
state_enabled:控件是否可用
state_checkable:控件可否被勾选,eg:checkbox
state_checked:控件是否被勾选
state_selected:控件是否被选择,针对有滚轮的情况
state_pressed:控件是否被按下
state_active:控件是否处于活动状态,eg:slidingTab
state_single:控件包含多个子控件时,确定是否只显示一个子控件
state_first:控件包含多个子控件时,确定第一个子控件是否处于显示状态
state_middle:控件包含多个子控件时,确定中间一个子控件是否处于显示状态
state_last:控件包含多个子控件时,确定最后一个子控件是否处于显示状态

运行效果图:

不同效果的按钮

二、单选扭(RadioButton)和复选框(CheckBox)的功能与用法

RadioButton、CheckBox 都继承自 Button 类,但与普通按钮不同的是,他们都多了一个可选中的功能,因此他们可以额外指定一个 android:checked 属性,该属性用于指定 RadioButton、CheckBox 初始化的时候是否被选中。

栗子:用 RadioButton、CheckBox 获取用户信息

  • MainActivity.java
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // 获取界面上rg、show两个组件
        rg = (RadioGroup) findViewById(R.id.rg);
        show = (TextView) findViewById(R.id.show);
        // 为RadioGroup组件的OnCheckedChange事件绑定事件监听器
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // 根据用户勾选的单选按钮来动态改变tip字符串的值
                String tip = checkedId == R.id.male ? "您的性别是男人": "您的性别是女人";
                // 修改show组件中的文本
                show.setText(tip);
            }
        });
    }

上面的代码采用了“委托式”的事件处理机制,其原理是:当事件源上发生事情的时候,该事件会激发该事件源上的监听器的特定方法。
- main.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性别:"/>
        <!-- 定义一组单选按钮 -->
        <RadioGroup android:id="@+id/rg"
            android:orientation="horizontal"
            android:layout_gravity="center_horizontal">
            <!-- 定义两个单选按钮 -->
            <RadioButton android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:id="@+id/male"
                         android:text="男"
                         android:checked="true"/>
            <RadioButton android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:id="@+id/female"
                         android:text="女"/>
        </RadioGroup>
    </TableRow>
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="喜欢的颜色:"/>
        <!-- 定义一个垂直的线性布局 -->
        <LinearLayout android:layout_gravity="center_horizontal"
                      android:orientation="vertical"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content">
            <!-- 定义三个复选框 -->
            <CheckBox android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="红色"
                      android:checked="true"/>
            <CheckBox android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="蓝色"/>
            <CheckBox android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="绿色"/>
        </LinearLayout>
    </TableRow>
    <TextView
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</TableLayout>

如果在布局文件中指定了某个按钮默认选中的话,则必须为该组单选按钮指定android:id属性,否则不能正常工作。

运行效果:

RadioButton、CheckBox

三、ToggleButton 和 Switch 的功能与用法

ToggleButton 和 Switch 也是由 Button 派生而来的,因此 Button 的相关属性在这里也都起作用,这里给出更详尽的属性:

1、ToggleButton属性:
  • android:checked:设置该按钮是否被选中
  • android:textOff:设置该按钮状态关闭时显示的文本
  • android:textOn:设置该按钮状态打开时显示的文本
2、Switch属性:
  • android:showText:设置on/off的时候是否显示文字,boolean
  • android:splitTrack:是否设置一个间隙,让滑块与底部图片分隔,boolean
  • android:switchMinWidth:设置开关的最小宽度
  • android:switchPadding:设置滑块内文字的间隔
  • android:switchTextAppearance:设置开关的文字外观,暂时没发现有什么用…
  • android:textOff:按钮没有被选中时显示的文字
  • android:textOn:按钮被选中时显示的文字
  • android:textStyle:文字风格,粗体,斜体写划线那些
  • android:track:底部的图片
  • android:thumb:滑块的图片
  • android:checked:设置该按钮是否被选中
  • android:textOff:设置该按钮状态关闭时显示的文本
  • android:textOn:设置该按钮状态打开时显示的文本
  • android:typeface:设置字体,默认支持这三种:sans、serif、monospace;除此以外还可以使用 其他字体文件(*.ttf),首先要将字体文件保存在assets/fonts/目录下,不过需要在Java代码中设置:Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"); textView.setTypeface(typeFace);

联系方式:

简书:WillFlow
CSDN:WillFlow
微信公众号:WillFlow

微信公众号:WillFlow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值