title: Android Button的基本使用

tags: Button,按钮

Button介绍:

Button(按钮)继承自TextView,在Android开发中,Button是常用的控件,用起来也很简单,你可以在界面xml描述文档中定义,也可以在程序中创建后加入到界面中,其效果都是一样的。不过最好是在xml文档中定义,因为一旦界面要改变是话,直接修改一下xml就行了,不用修改Java程序,并且在xml中定义层次分明,一目了然。

Button 支持的 XML 属性及相关方法

XML 属性 相关方法 说明
android:clickable setClickable(boolean clickable) 设置是否允许点击。
clickable=true:允许点击
clickable=false:禁止点击
android:background setBackgroundResource(int resid) 通过资源文件设置背景色。
resid:资源xml文件ID
按钮默认背景为android.R.drawable.btn_default
android:text setText(CharSequence text) 设置文字
android:textColor setTextColor(int color) 设置文字颜色
android:onClick setOnClickListener(OnClickListener l) 设置点击事件

下面通过实例来给大家介绍Button的常用效果。

实例:Button点击事件写法1、写法2、设置背景图片、设置背景颜色、设置背景shape、V7包按钮样式

我们首先来看一下布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:orientation="vertical"> <Button
android:id="@+id/btn_click_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button点击事件写法1" /> <Button
android:id="@+id/btn_click_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="Button点击事件写法2" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@mipmap/icon_button_bg"
android:padding="10dp"
android:text="Button设置背景图片" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@android:color/holo_red_dark"
android:padding="10dp"
android:text="Button设置背景颜色" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/shape_button_test"
android:padding="10dp"
android:text="Button设置shape" /> <TextView
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="V7包按钮样式"
android:textColor="#ffffffff"
android:textSize="20sp" /> </LinearLayout>

布局文件对应的效果图如下:

上面布局文件中定义了6个Button,它们指定的规则如下。

1.给Button指定了android:id="@+id/btn_click_one",在MainActivity.xml根据id进行查找并且设置点击事件。

//给第一个按钮设置点击事件
findViewById(R.id.btn_click_one).setOnClickListener(onClickListener);

点击之后进行Toast提示。

private View.OnClickListener onClickListener=new View.OnClickListener() {
@Override
public void onClick(View v){
Toast.makeText(MainActivity.this,"Button点击事件1",Toast.LENGTH_LONG).show();
}
};

2.给xml中给button增加了android:onClick="click"属性,然后在该布局文件对应的Acitivity中实现该方法。需要注意的是这个方法必须符合三个条件:

1).方法的修饰符是 public

2).返回值是 void 类型

3).只有一个参数View,这个View就是被点击的这个控件。

     public void click(View v){
switch (v.getId()){
case R.id.btn_click_two:
Toast.makeText(MainActivity.this,"Button点击事件2",Toast.LENGTH_LONG).show();
break;
}
}

3.设置一张背景图片

android:background="@mipmap/icon_button_bg"

4.设置背景颜色

android:background="@android:color/holo_red_dark"

5.设置背景shape,android:background="@drawable/shape_button_test",可以自定义Button的外观,从效果图中我们可以看到Button背景透明,有边框,有弧度。

shape_button_test.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 默认背景色 -->
<solid android:color="@android:color/transparent"/>
<!-- 边框 -->
<stroke
android:width="1dp"
android:color="@android:color/black" />
<!-- 设置弧度 -->
<corners
android:radius="20dp"/>
</shape>

6.设置按钮的样式

style="@style/Widget.AppCompat.Button.Colored"

这是V7包里面自带的style样式。按钮的颜色是ButtonTest/app/src/main/res/values/colors.xml下name="colorAccent"的颜色。

Button使用注意事项:

1.Button的setOnClickListener优先级比xml中android:onClick高,如果同时设置点击事件,只有setOnClickListener有效。

2.能用TextView就尽量不要用Button,感觉TextView灵活性更高。(纯属个人意见)

学到了以上几招,能解决开发中Button的大部分用法。

点击下载源码

各位看官如果觉得文章不错,帮忙点个赞吧,对于你来说是举手之劳,但对于我来说这就是坚持下去的动力。

如果你想第一时间看我们的后期文章,扫码关注公众号,每周不定期推送Android开发实战教程文章,你还等什么,赶快关注吧,学好技术,出任ceo,赢取白富美。。。。

      Android开发666 - 安卓开发技术分享
扫描二维码加关注

Android Button的基本使用的更多相关文章

  1. android Button 切换背景,实现动态按钮和按钮颜色渐变

        android Button 切换背景,实现动态按钮和按钮颜色渐变 一.添加android 背景筛选器selector实现按钮背景改变     1.右键单击项目->new->Oth ...

  2. android 按钮特效 波纹 Android button effects ripple

    android 按钮特效 波纹 Android button effects ripple 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E- ...

  3. 我的Android进阶之旅------>android Button上面的英文字符串自动大写的问题解决

    今天碰到一个关于Button的问题:android Button上面的英文字符串会自动变成大写,运行的Android 5.1版本,如下图所示: 图1:Button 图2:TextView 这个Butt ...

  4. Android Button Maker(在线生成android shape xml文件的工具),真方便!

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/scry5566/article/details/25379275        直接上地址:http ...

  5. 我的Android进阶之旅------&gt;android Button上面的英文字符串自己主动大写的问题解决

    今天碰到一个关于Button的问题:android Button上面的英文字符串会自己主动变成大写,执行的Android 5.1版本号,例如以下图所看到的: 图1:Button 图2:TextView ...

  6. Android Button点击效果(按钮背景变色、文字变色)

    一. 说明 Android Button的使用过程中,我们会需要为Button添加点击效果,不仅仅按钮的背景色需要变化,而且有时,我们连文字的颜色都希望变化,我们可以使用StateListDrawab ...

  7. android button minheight问题

    Android的button控件默认在内部text周围是有padding的,而且不受控制,这样子看似button控件在高度/宽度上像是被拉伸了,如何解决这个问题? 只要在xml中设置MinHeight ...

  8. android button 函数调用栈

    Button button=(Button) findViewById(R.id.button);button.setOnClickListener(new Button.OnClickListene ...

  9. android button 字母自动大写

    <Button android:id="@+id/btnStart" android:layout_width="wrap_content" androi ...

随机推荐

  1. Visual Studio 2010的MSDN帮助文档离线使用

    如果没有在安装vs过程中安装帮助,也可通过Visual Studio帮助菜单中的Manage Help Settings来对帮助进行设置或安装. 可以选择从磁盘安装内容,如果选择从磁盘安装可能会要求提 ...

  2. 用scikit-learn进行LDA降维

    在线性判别分析LDA原理总结中,我们对LDA降维的原理做了总结,这里我们就对scikit-learn中LDA的降维使用做一个总结. 1. 对scikit-learn中LDA类概述 在scikit-le ...

  3. SignalR系列续集[系列8:SignalR的性能监测与服务器的负载测试]

    目录 SignalR系列目录 前言 也是好久没写博客了,近期确实很忙,嗯..几个项目..头要炸..今天忙里偷闲.继续我们的小系列.. 先谢谢大家的支持.. 我们来聊聊SignalR的性能监测与服务器的 ...

  4. 46张PPT讲述JVM体系结构、GC算法和调优

    本PPT从JVM体系结构概述.GC算法.Hotspot内存管理.Hotspot垃圾回收器.调优和监控工具六大方面进行讲述.(内嵌iframe,建议使用电脑浏览) 好东西当然要分享,PPT已上传可供下载 ...

  5. Java中Comparable与Comparator的区别

    相同 Comparable和Comparator都是用来实现对象的比较.排序 要想对象比较.排序,都需要实现Comparable或Comparator接口 Comparable和Comparator都 ...

  6. C#多线程之线程池篇3

    在上一篇C#多线程之线程池篇2中,我们主要学习了线程池和并行度以及如何实现取消选项的相关知识.在这一篇中,我们主要学习如何使用等待句柄和超时.使用计时器和使用BackgroundWorker组件的相关 ...

  7. 【开源】.Net 动态脚本引擎NScript

    开源地址: https://git.oschina.net/chejiangyi/NScript 开源QQ群: .net 开源基础服务  238543768 .Net 动态脚本引擎 NScript   ...

  8. .Net 大型分布式基础服务架构横向演变概述

    一. 业务背景 构建具备高可用,高扩展性,高性能,能承载高并发,大流量的分布式电子商务平台,支持用户,订单,采购,物流,配送,财务等多个项目的协作,便于后续运营报表,分析,便于运维及监控. 二. 基础 ...

  9. Ajax实现原理,代码封装

    都知道实现页面的异步操作需要使用Ajax,那么Ajax到是怎么实现异步操作的呢? 首先需要认识一个对象 --> XMLHttpRequest 对象 --> Ajax的核心.它有许多的属性和 ...

  10. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...