Android Button常用法
常用属性:
<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常用法的更多相关文章
- Android Button的基本使用
title: Android Button的基本使用 tags: Button,按钮 --- Button介绍: Button(按钮)继承自TextView,在Android开发中,Button是常用 ...
- Android之Adapter用法总结-(转)
Android之Adapter用法总结 1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(List View,Grid Vie ...
- Android之Adapter用法总结(转)
Android之Adapter用法总结 1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(List View,Grid Vie ...
- Android之Adapter用法总结
http://blog.csdn.net/fznpcy/article/details/8658155 Android之Adapter用法总结 1.概念 Adapter是连接后端数据和前端显示的适配器 ...
- Android状态选择器用法总结
原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6284682.html 本文首先列出常见状态选择器的创建,然后按照常用控件来分别列出状态选择器的具体 ...
- ZT onActivityResult在android中的用法
onActivityResult在android中的用法 举例说我想要做的一个事情是,在一个主界面(主Activity)上能连接往许多不同子功能模块(子Activity上去),当子模块的事情做完之后就 ...
- 【Android学习】android:layout_weight的用法实例
对于android:layout_weight的用法,用下面的例子来说明: <LinearLayout xmlns:android="http://schemas.android.co ...
- Android webservice的用法详细讲解
Android webservice的用法详细讲解 看到有很多朋友对WebService还不是很了解,在此就详细的讲讲WebService,争取说得明白吧.此文章采用的项目是我毕业设计的webserv ...
- android Button 切换背景,实现动态按钮和按钮颜色渐变
android Button 切换背景,实现动态按钮和按钮颜色渐变 一.添加android 背景筛选器selector实现按钮背景改变 1.右键单击项目->new->Oth ...
随机推荐
- Ant Design按需加载
不eject情况下配置antd按需加载 1.安装 react-app-rewired yarn add react-app-rewired 2. 项目根目录下新建 config-overrides.j ...
- 【升鲜宝】生鲜配送管理系统_升鲜宝 V2.0 按客户商品分类分开打印配送与按客户商品分类导出相关订单商品相关说明(一)
[升鲜宝]生鲜配送管理系统_升鲜宝 V2.0 按[客户]的商品分类分开打印(配送单)与按[客户]商品分类[对账单]导出相关销售订单商品功能相关说明(一) 业务场景概述与痛点 1.中小学校食堂的客户,每 ...
- 一起学Android之ListView
本文以一个小例子,简述Android开发中ListView的相关应用,仅供学习分享使用. 概述 ListView是一个显示可滚动项目列表的视图组(view group),列表项通过适配器(Adapte ...
- Hadoop Yarn框架原理解析
在说Hadoop Yarn的原理之前,我们先来看看Yarn是怎样出现的.在古老的Hadoop1.0中,MapReduce的JobTracker负责了太多的工作,包括资源调度,管理众多的TaskTrac ...
- C# 虚拟串口通信
将主端口COM8拆分成 COM1和COM2两个虚拟端口 COM8接收的消息会传递给COM1和COM2 SerialPort spSend;//spSend,spReceive用虚拟串口连接,它们之间可 ...
- Thymeleaf 的 onclick
th:onclick="'javascript:openBox(\''+${curCabNo}+'\',\''+${box.no}+'\')'" 真的难受 ,有没有好办法!!!!
- RuntimeException和Exception区别
1.java将所有的错误封装为一个对象,其根本父类为Throwable, Throwable有两个子类:Error和Exception. 2.Error是Throwable 的子类,用于指示合理的应用 ...
- oracle异地备份
一.安装oracle客户端 右键以管理员身份运行 选择管理员 跳过软件更新 选择语言,默认中文 指定安装位置 检查当前环境 安装 二.使用exp命令备份 exp 用户名/密码@IP地址/数据库 own ...
- C# -- 使用 DriveInfo 获取磁盘驱动器信息
C# -- 使用 DriveInfo 获取磁盘驱动器信息 1. 代码实现 class Program { static void Main(string[] args) { GetComputerDi ...
- 每日PA -2019年1月帖-每天更新
开篇 "每日PA"有什么亮点?