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. webp图片实践之路

    最近,我们在项目中实践了webp图片,并且抽离出了工具模块,整合到了项目的基础模板中.传闻IOS10也将要支持webp,那么使用webp带来的性能提升将更加明显.估计在不久的将来,webp会成为标配. ...

  2. Visual Studio Code 代理设置

    Visual Studio Code (简称 VS Code)是由微软研发的一款免费.开源的跨平台文本(代码)编辑器,在十多年的编程经历中,我使用过非常多的的代码编辑器(包括 IDE),例如 Fron ...

  3. Apache 与 php的环境搭建

    Apache和PHP的版本分别为: httpd-2.4.9-win64-VC11.zip php-5.6.9-Win32-VC11-x64.zip 下载地址: php-5.6.9-Win32-VC11 ...

  4. 让 windows 下的命令行程序 cmd.exe 用起来更顺手

    在 Windows 下使用 Larave 框架做开发,从 Composer 到 artisan 总是避免不了和 cmd.exe 打交道,系统默认的命令行界面却是不怎么好看,且每行显示的字符数是做了限制 ...

  5. 在Ubuntu 16.10 安装 git 并上传代码至 git.oschina.net

    1. 注册一个账号和创建项目 先在git.oschina.net上注册一个账号和新建一个project ,如project name 是"myTest". 2.安装git sudo ...

  6. 搭建属于自己的VIP积分系统(1)

    很久没写博客了,如果有写得不好的地方,还请多多见谅. 架构设计 需求分析 这篇文章主要是介绍此VIP系统的基础架构.说实在的,我其实对 架构方面也不是很懂,我这套框架 还是拿别人的东西改过来的,并不是 ...

  7. 水平可见直线 bzoj 1007

    水平可见直线 (1s 128M) lines [问题描述] 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆 ...

  8. 创建ABPboilerplate模版项目

    本文是根据角落的白板报的<通过ABPboilerplate模版创建项目>一文的学习总结,感谢原文作者角落的白板报. 1 准备 开发环境: Visual Studio 2015 update ...

  9. Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)

    昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...

  10. HotApp小程序服务范围资质查询器

    微信小程序提交审核需要选择资质服务范围,如果服务范围不对,审核会不通过, 开发小程序之前,最好先查询所开发小程序的资质范围,否则无法通过微信审核.   小程序的资质范围查询地址,数据同步微信官方 ht ...