关于Android较常用的组件介绍

 之前给大家讲了安卓的基本组件,很明显那是不够的,因此我在这篇帖子再补充一些较常用的组件。

1、RadioButton(单选按钮)和CheckBox(多选按钮)

我们通常会用到单选和多选按钮,比如用户注册页面可能出现性别选择的单选按钮和兴趣爱好选择的多选按钮。他们都是继承于Button,可以直接使用Button的属性和方法。但是他们多了一个可选中的功能,因此他们有一个checked属性,用来指定他们初始状态时是否被选中。当然RadioButton和CheckBox之间是有区别的,因为一组RadioButton中只能有一个被选中,而CheckBox可以同时选择多个,这也就使得当我们写RadioButton组的时候,外面要写一个RadioGroup。下面我就贴一些主要的关于他们的代码:

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginLeft="40dp"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/rbt_girl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/girl" />

            <RadioButton
                android:id="@+id/rbt_boy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="@string/boy" />
        </RadioGroup>


    <CheckBox
        android:id="@+id/cb_sing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="130dp"
        android:text="@string/sing" />

    <CheckBox
        android:id="@+id/cb_sing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="130dp"
        android:text="@string/dance" />

    <CheckBox
        android:id="@+id/cb_sing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="130dp"
        android:text="@string/read" />
要注意的是,我们在上面的代码中让性别为男默认为选中,而多选按钮则一个都没选。如果单选按钮有一个选中,则其他的之前不管是被选中的还是用户自己选中的,都要变成没被选中。
2、ToggleButton

ToggleButton和CheckBox很相似,他们都可以提供两个状态,但不同的是TaggleButton通常用于切换程序中的某种状态。就像我们通常会开启一个服务,我们可以使用ToggleButton让它成为一个开关,而它也通常被设计成开关的样子,这个详见后图。

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/togglebutton"
        android:textOff=""
        android:textOn="" />

3、AnalogClock(数字时钟)和DigitalClock(模拟时钟)

Android提供了这两个时间组件,他们会显示当前的时间,能够特别方便开发者的使用,功能相当于安卓系统自带的时钟。除此之外,Android还提供一个叫Chronometer的计时器组件,可以说是我们手机上的秒表的原型,因为它是显示从某个时间开始过了多少时间,它有几个特殊的方法,方便用户的计时,包括:

setBase(long base):设置起始时间

setFormat(String format):设置时间显示的格式

start():开始计时

stop():停止计时

<strong style="font-size:14px;">    </strong><span style="font-size:12px;"><AnalogClock 
        android:id="@+id/ac_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp" />
    
    <DigitalClock 
        android:id="@+id/dc_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="10dp"
        android:textColor="@color/holo_blue_bright"
        android:textSize="16sp"/></span>

这大概就是几种用的还算多的组件,下面我展示一下我使用了这几种组件的一个注册页面,大家都去动手写一下会比较好,其实很简单,当然也有点丑咯。


在这里,我还要插播一个关于边距的,也就是margin和padding,我自己也在这个上面混淆不清过。那么他们之间有什么区别呢?

margin是标注了这个属性的组件和其他组件之间的距离,而padding是自己和自己的边框之间的距离,比如说按钮里面的文字和它的边框之间的距离。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值