Android控件之Button
来源:互联网 发布:golang 调用c代码 编辑:程序博客网 时间:2024/06/05 16:57
Button 控件
前言:
安卓按钮是使用的比较多的一个控件了,在这里对Android Button进行讲解。
Button继承自TextView,所以也带有TextView的一些属性。
Button的直接子类为CompoundButton,Button的间接子类有CheckButton,RadioButton,Switch和ToggleButton。
Button常用属性设置
1.1设置Button的背景颜色
要设置Button按钮的背景颜色,可以通过在xml文件中设置android:background属性实现,
同样也可以通过在代码中使用setBackgroundColor()方法来实现。
1.2设置Button的文字颜色
要设置Button按钮的文字颜色,可以通过在xml文件中设置android:textColor属性实现,
同样也可以通过在代码中使用setTextColor()方法来实现。
1.3设置Button的文字格式
要设置Button按钮的文字格式,可以通过在xml文件中设置android:textStyle属性实现。
其中,参数italic表示斜体,参数bold表示粗体。
1.4设置Button的背景图片
要设置Button按钮的背景图片,可以通过在xml文件中设置android:background属性实现,
同样也可以通过在代码中使用setBackgroundResource()方法来实现。
2.Button事件监听器
Button的常用事件监听器有以下一些:
mButton.setOnClickListener();//点击事件监听器
mButton.setOnTouchListener();//触摸事件监听器
mButton.setOnFocusChangeListener();//焦点状态改变事件监听器
mButton.setOnKeyListener();//按键事件监听器
mButton.setOnLongClickListener();//常压事件监听器
这些事件监听器可以用来响应对Button按钮的不同操作,使用方法比较简单,此处就不多介绍了。
3.Button按钮图文混排
3.1通过设置android:drawableTop等属性来实现
在xml文件中,想要实现图片环绕文字的效果,可以通过设置以下四个属性来实现。
android:drawableTop设置文字上方显示的图片
android:drawableBottom设置文字下方显示的图片
android:drawableLeft显示文字左边显示的图片
android:drawableRight显示文字右边显示的图片
布局XML内容如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.dsl.ui_application_06.MainActivity"> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="60dp" android:drawableTop="@android:drawable/arrow_up_float" android:drawableBottom="@android:drawable/arrow_down_float" android:text="嗨" android:textSize="14sp" android:background="@color/colorAccent" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="60dp" android:layout_below="@id/button1" android:layout_marginTop="30dp" android:drawableTop="@android:drawable/arrow_up_float" android:drawableBottom="@android:drawable/arrow_down_float" android:text="你好" android:textSize="16sp" android:background="@color/colorPrimary" /></RelativeLayout>
显示效果如下:
接下来就是神器的一刻了、当我们在使用日常app的时候细心的朋友应该能发现,当我在登录账号的时候、按下登录按钮、
这个按钮就发生了变化,有些是大小变化了、有些是边框变化了、有些是背景颜色变化了。
接下来咱们就来做一个按钮被按下时背景颜色变化,感兴趣的可以做自行研究下其它两种。
Button按下和没有被按下时按钮显示不同的背景颜色
首先在drawable中新建一个selector标签的xml文件,如下:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:drawable="@android:drawable/ic_btn_speak_now"></item> <item android:state_pressed="false" android:drawable="@android:drawable/ic_input_delete"></item></selector>
selector标签下的item标签 可以设置如下属性:android:state_xxx 各种状态(值为true/false) 如果值为false的话,
android:state_xxx可以省略state_pressed
按钮按下的状态android:drawable 该状态下对应显示的图片
在布局文件中的引用:android:background="@draable/button_elector"
原内容如下:
<Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="60dp" android:layout_below="@id/button1" android:layout_marginTop="30dp" android:drawableTop="@android:drawable/arrow_up_float" android:drawableBottom="@android:drawable/arrow_down_float" android:text="你好" android:textSize="16sp" android:background="@drawable/button_selector" />
效果如下:
关于Android按钮控件的讲解到此结束!
源码地址如下:
https://github.com/DSLAndroid/UI_Application_06
本资源来自单胜凌!!!
Android靠自学!!!
祝各位IT人士早日取得成功!!!
- Android控件之Button
- Android控件之Button
- Android控件之Button
- Android控件之Button
- android控件之Button
- Android 控件开发之Button
- Android之Button控件学习
- Android 控件开发之Button
- Android控件之二 (Button)
- Android之自定义Button控件
- android常用控件学习之Button
- android控件Button之简单使用
- Android开发之自定义控件-自定义Button
- Android基本控件之Button和ImageButton
- Android控件详解之Button和Checkbox
- Android基础UI控件之Button
- Android:基础控件之(一)Button
- Android系列之自定义Button控件
- VS中关于预编译头文件的介绍
- Java中十进制,二进制,十六进制的相互转换总结
- 简易select二级联动
- Android 6.0 sensor 框架详解 (application层)
- 解决“打包后微信分享不能使用”的问题
- Android控件之Button
- mac osx运行docker出现Cannot connect to the Docker daemon. Is the docker daemon running on this host?
- 保存图片信息到数据库,并可读取后显示在前端
- 怎么在Eclipse中指定jvm?,配置Eclipse.ini文件
- TF-IDF及其算法
- 汇编程序之分离奇偶数
- configure: error: xml2-config not found. Please check your libxml2 installation
- Handler原理剖析
- 我的记录