Android Button的基本使用
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的基本使用的更多相关文章
- android Button 切换背景,实现动态按钮和按钮颜色渐变
android Button 切换背景,实现动态按钮和按钮颜色渐变 一.添加android 背景筛选器selector实现按钮背景改变 1.右键单击项目->new->Oth ...
- android 按钮特效 波纹 Android button effects ripple
android 按钮特效 波纹 Android button effects ripple 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E- ...
- 我的Android进阶之旅------>android Button上面的英文字符串自动大写的问题解决
今天碰到一个关于Button的问题:android Button上面的英文字符串会自动变成大写,运行的Android 5.1版本,如下图所示: 图1:Button 图2:TextView 这个Butt ...
- Android Button Maker(在线生成android shape xml文件的工具),真方便!
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/scry5566/article/details/25379275 直接上地址:http ...
- 我的Android进阶之旅------>android Button上面的英文字符串自己主动大写的问题解决
今天碰到一个关于Button的问题:android Button上面的英文字符串会自己主动变成大写,执行的Android 5.1版本号,例如以下图所看到的: 图1:Button 图2:TextView ...
- Android Button点击效果(按钮背景变色、文字变色)
一. 说明 Android Button的使用过程中,我们会需要为Button添加点击效果,不仅仅按钮的背景色需要变化,而且有时,我们连文字的颜色都希望变化,我们可以使用StateListDrawab ...
- android button minheight问题
Android的button控件默认在内部text周围是有padding的,而且不受控制,这样子看似button控件在高度/宽度上像是被拉伸了,如何解决这个问题? 只要在xml中设置MinHeight ...
- android button 函数调用栈
Button button=(Button) findViewById(R.id.button);button.setOnClickListener(new Button.OnClickListene ...
- android button 字母自动大写
<Button android:id="@+id/btnStart" android:layout_width="wrap_content" androi ...
随机推荐
- VM(虚拟机安装win7 提示 :units specified don't exist, SHSUCDX can't install)解决方法
改成IDE的模式
- ASP.NET Core 中文文档 第四章 MVC(3.8)视图中的依赖注入
原文:Dependency injection into views 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:孟帅洋(书缘) ASP.NET Core 支持在视图中使用 依赖 ...
- C#4.0泛型的协变,逆变深入剖析
C#4.0中有一个新特性:协变与逆变.可能很多人在开发过程中不常用到,但是深入的了解他们,肯定是有好处的. 协变和逆变体现在泛型的接口和委托上面,也就是对泛型参数的声明,可以声明为协变,或者逆变.什么 ...
- iOS开源项目周报0105
由OpenDigg 出品的iOS开源项目周报第四期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开发方面的开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等. He ...
- Log4net - 规则简介
参考页面: http://www.yuanjiaocheng.net/CSharp/csharprumenshili.html http://www.yuanjiaocheng.net/entity/ ...
- 【深入Java虚拟机】之四:类加载机制
类加载过程 类从被加载到虚拟机内存中开始,到卸载出内存为止,它的整个生命周期包括:加载.验证.准备.解析.初始化.使用和卸载七个阶段.它们开始的顺序如下图所示: 其中类加载的过程包括了加载.验 ...
- [数据结构]——堆(Heap)、堆排序和TopK
堆(heap),是一种特殊的数据结构.之所以特殊,因为堆的形象化是一个棵完全二叉树,并且满足任意节点始终不大于(或者不小于)左右子节点(有别于二叉搜索树Binary Search Tree).其中,前 ...
- App 审核由于 IPv6 网络问题被拒
昨天 提交App Store 的时候被拒了 We discovered one or more bugs in your app when reviewed on iPhone running iOS ...
- Atitit.技术管理者要不要自己做开发??
Atitit.技术管理者要不要自己做开发?? 1. 为什么很多管理者不能自己亲自做了1 1.1. 沟通成本多了1 1.2. .组织分散. 1 1.3. 会议多 .协调多 1 1.4. 问题的根源在于我 ...
- WINDOWS系统下MYSQL安装过程中的注意事项
1.首先MySQL的安装方式有两种:一种是MSI安装方式,很简单就像安装Windows软件一样.另外一种就是ZIP安装方式.这种相对而言比较麻烦.新手推荐MSI安装方式. 安装方式有以下两种: MSI ...