常用属性:

<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. es6基础知识

    1.超引用:(...) 用于取出参数对象中的所有可遍历属性,拷贝到当前对象之中 function fun(...args){ console.log(args); //[1,2,3,4,5,6] ar ...

  2. Apache2配置多域名站点及支持https

    0x00 预备条件 申请SSL证书 建立对应站点目录 开放443端口 0x01 配置sites-available文件 执行 vi /etc/apache2/sites-available/zecoc ...

  3. 基于python的种子搜索网站-开发过程

    本讲会对种子搜索网站的开发过程进行详细的讲解. 源码地址:https://github.com/geeeeeeeek/bt 项目开发过程 项目简介 该项目是基于python的web类库django开发 ...

  4. Oracle字符到数值转换错误

    [错误] [问题分析] line 3: 定义 NUM_VAL varchar2(500); line 9: NUM_VAL := 'NUM'+1; NUM_VAL是一个varchar类型的数据,而在数 ...

  5. Python 经典面试题汇总之基础篇

    基础篇 1:为什么学习Python 公司建议使用Python,然后自己通过百度和向有学过Python的同学了解了Python.Python这门语言,入门比较简单,它简单易学,生态圈比较强大,涉及的地方 ...

  6. [转载]——说说IO

    本文转载自"和你在一起"的"说说IO"系列文章https://pengjiaheng.iteye.com/,总共分为8篇,特意整理.收录在此,支持原创.尊重原创 ...

  7. sql 按年月日统计

    1.每年select year(ordertime) 年,sum(Total) 销售合计from 订单表group by year(ordertime) 2.每月select year(orderti ...

  8. CSS3中三角形及三角形组合图实现

        几何之三角形及三角形的组合图案理论 三角形( triangle ['traɪæŋɡl])可以看成正方形对角线交叉形成的图形 若想得到编号①方向向下三角形,只需对编号②③④三角形让其透明tran ...

  9. C# out ref 用法总结

    C#里面的 out 和ref参数时常会用到,但对它们的区别比较模糊.所以总结一下.下面是测试代码: public void Start() { //outSum没必要赋值,赋值了也完全没用. //如果 ...

  10. 11 Django REST Framework 针对基于类的视图添加 @csrf_exempt

    01-在类的 dispatch 方法上使用 @csrf_exempt from django.views.decorators.csrf import csrf_exempt class MyView ...