(五)Android中的Button组件

1.主要内容

  1. 文字大小和颜色
  2. 自定义背景形状
  3. 自定义按压效果
  4. 点击事件

2.具体实现

1.Button是继承自TextView,所以设置按钮中的文字大小和颜色与上一个相同

<Button
        android:layout_marginTop="15dp"
        android:id="@+id/mybutton1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮一"
        android:textSize="15sp"
        android:textColor="#000000"
        android:background="#FF0000"
        ></Button>

2.为了更加美观,设置曲边矩形会更漂亮。在drawable中新建一个文件。
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:color="#FF9900"/>
    <corners
        android:radius="50dp"/>
</shape>

在这里插入图片描述
这样得到的是一个实心的按钮,通过下面的xml内容,将其更改为只有边的

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="5dp"
        android:color="#FF9900"/>
    <corners
        android:radius="50dp"/>
</shape>

有些时候我们需要为了强调确实已经点击了按钮,即做到点击之后和原先的效果不一样,这就要设置如下xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
    <shape>
        <solid android:color="#66FF00"></solid>
        <corners android:radius="50dp"></corners>
    </shape>
</item>
    <item android:state_pressed="false">
        <shape>
            <solid android:color="#09F7F7"></solid>
            <corners android:radius="50dp"></corners>
        </shape>
    </item>
</selector>

这里通过直接添加属性的方法注册点击事件
在这里插入图片描述
然后编写Java代码

public void showToast(View view){
        Toast.makeText(this,"我哦是按钮四",Toast.LENGTH_SHORT).show();
    }

即可实现效果,如下所示:、
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值