常用属性:

<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. WOW.js 使用教程

    官网加动画特效,哇哦,下面我介绍一下WOW.js 官网地址:https://www.delac.io/wow/ 点击github可以找到wow.js和wow.min.js 以及animate.css者 ...

  2. Web前端-CSS必备知识点

    Web前端-CSS必备知识点 css基本内容,类选择符,id选择符,伪类,伪元素,结构,继承,特殊性,层叠,元素分类,颜色,长度,url,文本,字体,边框,块级元素,浮动元素,内联元素,定位. 链接: ...

  3. SuperMap iObject入门开发系列之一组件式GIS开发平台介绍

    本文是一位好友“炀炀”授权给我来发表的,介绍都是他的研究成果,在此,非常感谢.平台介绍:SuperMap iObjects Java/.NET 是面向GIS应用系统开发者的组件式GIS开发平台,具有强 ...

  4. K3实现按虚拟件/组件发料

           最近公司调度部反应了一个K3使用过程中遇到的巨大问题:公司成品BOM组成物料种类破千,绝大部分还不能拆分成组件.为了方便区分,从逻辑上把一堆堆的半成品分成了一个个虚拟件.K3生成的投料单 ...

  5. linux添加crontab定时任务

    1.crontab -e命令进入linux定时任务编辑界面,举个简单的例子,比如我要定时往txt文件写入 */ * * * * .txt */1就是每隔一分钟像文件写入,其他一些详细的操作大家可以去网 ...

  6. rocketmq批量消息投递

    批量发送消息可提高传递小消息的性能.同时也需要满足以下特征 批量消息要求必要具有同一topic.相同消息配置 不支持延时消息 建议一个批量消息最好不要超过1MB大小 示例 小于1MB String t ...

  7. python-对requests请求简单的封装

    # coding:utf-8 import requests class send_request: def __init__(self,url,method,data=None): self.res ...

  8. Win32 Ime

    Win32 Ime API: ImmGetContext: 获取指定窗口的当前的输入上下文,然后再尝试访问上下文中的信息.应用程序应该定期使用这个功能获取窗口的当前的输入上下文.若hWnd参数为零,将 ...

  9. Kafka Tuning Recommendations

    Kafka Brokers per Server Recommend 1 Kafka broker per server- Kafka not only disk-intensive but can ...

  10. 类 Calendar

    简介 Java.util.Calendar是日历类,在Date后出现,替换掉了许多Date的方法.该类将所有可能用到的时间信息封装为静态成员变量,方便获取.日历类就是方便获取各个时间属性的.注意Cal ...