常用属性:

<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮1"
android:textSize="20sp"
android:textColor="#0066ff"
android:background="#ff0000"/>
textSize:文字大小
textColor:文字颜色
background:背景颜色

drawable:

drawble应用:
1.绘制一个按钮,圆角,橙色背景
<Button
android:id="@+id/btn_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮2"
android:textSize="20sp"
android:textColor="#0066ff"
android:background="@drawable/bg_btn2"
android:layout_below="@+id/btn_1"
android:layout_marginTop="10dp"/>
引入drawable的xml文件

<?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="10dp"/> --圆角的角度
</shape>

-------------------------------------------------------------------------------------
2.绘制一个只有描边颜色的圆角按钮
<Button
android:id="@+id/btn_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮3"
android:textSize="20sp"
android:textColor="#0066ff"
android:layout_below="@+id/btn_2"
android:background="@drawable/bg_btn3"
android:layout_marginTop="10dp"/>
drawable文件内容:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> --矩形
    <stroke  --边框线属性
android:width="2dp"
android:color="#ff0099"/>
<corners --定义圆角
android:radius="10dp"/>
</shape>
-------------------------------------------------------------------------------------
按钮添加点击效果:
1.希望按钮被点击时更换背景颜色
<Button
android:id="@+id/btn_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮4"
android:textSize="20sp"
android:textColor="#FF0033"
android:layout_below="@+id/btn_3"
android:background="@drawable/bg_btn4"
android:layout_marginTop="10dp"
android:onClick="showToast"/>

<?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="#00ff00"></solid>
<corners android:radius="10dp"></corners>
</shape>
</item> <item android:state_pressed="false"> --没被点击
<shape>
<solid android:color="#005f00"></solid>
<corners android:radius="10dp"></corners>
</shape>
</item>
</selector>

-------------------------------------------------------------------------------------
点击按钮弹toast
第一种(常用)
public class ButtonActivity extends AppCompatActivity {

    //声明控件
private Button button3;
private TextView tv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
//找到控件
button3= (Button) findViewById(R.id.btn_3);
tv= (TextView) findViewById(R.id.tv_1);
//给控件添加点击监听方法
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ButtonActivity.this, "uuuuuuuuu", Toast.LENGTH_SHORT).show();
}
}); tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ButtonActivity.this,"我是textView",Toast.LENGTH_LONG).show();
}
});
}
-------------------------------------------------------------------------------------

第二种(不常用)

1.在布局xml文件中主动调用方法

<Button
android:id="@+id/btn_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮4"
android:textSize="20sp"
android:textColor="#FF0033"
android:layout_below="@+id/btn_3"
android:background="@drawable/bg_btn4"
android:layout_marginTop="10dp"
android:onClick="showToast"/> 2.在activity中创建此方法
public void showToast(View view){
Toast.makeText(ButtonActivity.this, "hahah", Toast.LENGTH_SHORT).show();
}

Android Button常用法的更多相关文章

  1. Android Button的基本使用

    title: Android Button的基本使用 tags: Button,按钮 --- Button介绍: Button(按钮)继承自TextView,在Android开发中,Button是常用 ...

  2. Android之Adapter用法总结-(转)

    Android之Adapter用法总结 1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(List View,Grid Vie ...

  3. Android之Adapter用法总结(转)

    Android之Adapter用法总结 1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(List View,Grid Vie ...

  4. Android之Adapter用法总结

    http://blog.csdn.net/fznpcy/article/details/8658155 Android之Adapter用法总结 1.概念 Adapter是连接后端数据和前端显示的适配器 ...

  5. Android状态选择器用法总结

    原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6284682.html 本文首先列出常见状态选择器的创建,然后按照常用控件来分别列出状态选择器的具体 ...

  6. ZT onActivityResult在android中的用法

    onActivityResult在android中的用法 举例说我想要做的一个事情是,在一个主界面(主Activity)上能连接往许多不同子功能模块(子Activity上去),当子模块的事情做完之后就 ...

  7. 【Android学习】android:layout_weight的用法实例

    对于android:layout_weight的用法,用下面的例子来说明: <LinearLayout xmlns:android="http://schemas.android.co ...

  8. Android webservice的用法详细讲解

    Android webservice的用法详细讲解 看到有很多朋友对WebService还不是很了解,在此就详细的讲讲WebService,争取说得明白吧.此文章采用的项目是我毕业设计的webserv ...

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

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

随机推荐

  1. 二、Snapman多人协作电子表格之——软件下载安装与配置

    Snapman多人协作电子表格是一个即时工作系统. 一.软件下载地址 Snapman下载的官网地址:http://www.snapman.xyz 在官网下载Snapman主程序安装: snapman_ ...

  2. Android视频录制从不入门到入门系列教程(二)————显示视频图像

    1.创建一个空的工程,注意声明下列权限: <uses-permission android:name="android.permission.CAMERA"/> < ...

  3. 从零学习Fluter(八):Flutter的四种运行模式--Debug、Release、Profile和test以及命名规范

    从零学习Fluter(八):Flutter的四种运行模式--Debug.Release.Profile和test以及命名规范 好几天没有跟新我的这个系列文章,一是因为这两天我又在之前的基础上,重新认识 ...

  4. 搭建Linux虚拟服务器

    1.搭建Linux虚拟机环境安装VMware Workstation 14下载地址:https://www.cr173.com/soft/68480.html密钥:FF31K-AHZD1-H8ETZ- ...

  5. @Resource 与 @Service注解的区别

    pring中什么时候用@Resource,什么时候用@service当你需要定义某个类为一个bean,则在这个类的类名前一行使用@Service("XXX"),就相当于讲这个类定义 ...

  6. 【Spring Cloud笔记】Eureka注册中心增加权限认证

    在Spring Cloud通过Eureka实现服务注册与发现时,默认提供web管理界面,但是如果在生产环境暴露出来,会存在安全问题.为了解决这个问题,我们可以通过添加权限认证进行控制,具体步骤如下: ...

  7. DVWA-命令执行学习笔记

    DVWA-命令执行 原理: web服务器没有对用户提交的数据进行严格的过滤,造成调用操作系统的命令或者在操作系统恶意拼接拼接命令,以达到攻击者的目的. 1.将DVWA的级别设置为low 1.2查看源代 ...

  8. Java实现遍历N级树形目录结构

    最近挺忙,一直在做项目,然后有个树形目录结构需要返回给前端,这里给大家说一下实现的思路. 具体达到的效果类似: 一级目录A: 二级目录A: 三级目录: 四级目录: 文件.txt 二级目录B: 文件1. ...

  9. 转载:img是什么元素?置换元素?

    转载: https://blog.csdn.net/kingliguo/article/details/52643594 img是什么元素? 应是行内元素,判断一个元素是行内元素,还是块元素,无非就是 ...

  10. 02-MySQL基础

    MySQL基础 1.存储引擎 1.1MyISAM MySQL5.5以及之前默认存储引擎MyISAM 如果应用是以读操作和插入操作为主,只有很少的更新和删除操作,并且对事务的完整性.并发性要求不高,那么 ...