Button简单学习和使用

Button

声明和设置按钮的点击事件
    /**
     * 声明一个btn,用来Button的demo
     */
    private Button mBtnButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置一个视图
        setContentView(R.layout.activity_main);
        //找到button
        mBtnButton.findViewById(R.id.btn_button);
        //给这个button设置一个点击事件
        mBtnButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到Button演示界面
            }
        });
    }
文字大小、颜色
    <!--
    textSize 设置按钮字体的大小
    backgroundTint 设置按钮的背景颜色
    textColor 设置按钮字体的颜色
    -->
    <Button
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/btn_1"
        android:text="按钮1"
        android:textSize="20sp"
        android:textColor="#FFFFFF"
        android:backgroundTint="#FF0000"/>
效果展示

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2K6Bmu8h-1663258273384)(C:/Users/zhengbo/%E6%88%91%E7%9A%84%E5%AD%A6%E4%B9%A0/Typora%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/%E5%AE%89%E5%8D%93/image-20220914224525220.png)].

自定义背景形状
<?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="100dp"/>

</shape>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kfCrMbSH-1663258273386)(C:/Users/zhengbo/%E6%88%91%E7%9A%84%E5%AD%A6%E4%B9%A0/Typora%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/%E5%AE%89%E5%8D%93/image-20220915221906992.png)].

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--画一个边框的形状-->
    <stroke
        android:width="1dp"
        android:color="#FF9900"/>
    <corners
        android:radius="100dp"/>

</shape>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cL7r9gBs-1663258273387)(C:/Users/zhengbo/%E6%88%91%E7%9A%84%E5%AD%A6%E4%B9%A0/Typora%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/%E5%AE%89%E5%8D%93/image-20220915222253965.png)].

   <!--基于上面两种形状,将它们--> 
   <Button
        android:id="@+id/btn_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn_1"
        android:background="@drawable/bg_btn2"
        android:text="按钮2"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        android:layout_marginTop="10dp"/>

    <Button
        android:id="@+id/btn_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn_2"
        android:layout_marginTop="10dp"
        android:background="@drawable/bg_btn3"
        android:text="按钮3"
        android:textColor="#FF9900"
        android:textSize="20sp" />
效果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1WkdKAt0-1663258273389)(C:/Users/zhengbo/%E6%88%91%E7%9A%84%E5%AD%A6%E4%B9%A0/Typora%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/%E5%AE%89%E5%8D%93/image-20220915222642132.png)].

自定义按压效果,点击事件

方法一:

 //方法一在按钮的点击事件当中通过Toast的makeText方法设置点击事件
 mBtn3.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
         Toast.makeText(ButtonActivity.this, "btn3被点击了", Toast.LENGTH_SHORT).show();
     }
 });

方法二:

//方法二,java代码配置xml文件进行配置
//定义一个方法,在方法当中调用Toast.makeText接口
public  void showToast(View view) {
    Toast.makeText(this, "btn4被点击了", Toast.LENGTH_SHORT).show();
}
<!--
android:onClick 去调用我们上面定义的按压按钮的方法
-->
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btn_4"
    android:text="按钮4"
    android:textSize="20sp"
    android:textColor="#FFFFFF"
    android:background="@drawable/bg_btn4"
    android:layout_below="@id/btn_3"
    android:layout_marginTop="10dp"
    android:onClick="showToast"/>
<!--
这个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="#AA6600" />
            <corners android:radius="5dp" />
        </shape>
    </item>
    <!--设置不按压的时候的样子-->
    <item android:state_pressed="false">
        <shape>
            <solid android:color="#FF9900" />
            <corners android:radius="5dp" />
        </shape>
    </item>
</selector>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值