Button 使用详解

极力推荐文章:欢迎收藏
Android 干货分享

阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android
本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:
一、Button 的继承关系
二、Button 简单使用举例
三、自定义 Button 选择器
四、Button 点击事件
五、onClick属性 实现点击事件
一、Button 的继承关系
Button 继承 TextView,具体关系如下:
java.lang.Object
↳ android.view.View
↳ android.widget.TextView
↳ android.widget.Button
二、Button 简单使用举例
使用 xml 布局跟java代码动态设置TextView。
- 1.
xml布局如下:
<Button
android:id="@+id/button_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/self_destruct" />
java代码中使用方法如下:
Button OnClickListener方法实现如下:
public class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
}
});
}
}
三、 自定义 Button 选择器
自定义Button 选择器,可以更加友好的跟用户进行交互。
xml布局使用
<Button
android:id="@+id/btn_selector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/custom_btn_green_selector"
android:text="一、自定义Button背景选择器 "
android:textColor="@color/white" />
- 2.
Button背景选择器实现
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按下去的背景颜色显示效果 -->
<item android:drawable="@drawable/btn_pressed" android:state_pressed="true"/>
<!-- 获取焦点时背景颜色显示效果 -->
<item android:drawable="@drawable/btn_pressed" android:state_focused="true"/>
<!-- 没有任何状态下的背景颜色 -->
<item android:drawable="@drawable/btn_normal"/>
</selector>
java代码中点击实现 效果
public class ButtonMethod extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
// 一、自定义Button背景选择器、匿名内部类实现点击事件
Button mBtnSelector = (Button) findViewById(R.id.btn_selector);
mBtnSelector.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(ButtonMethod.this, "你点击了按钮选择器", 1).show();
}
});
// 一、自定义Button背景选择器、匿名内部类实现点击事件
}
}
- 4.
Button正常以及获取焦点图片素材


四、Button 点击事件
xml布局使用
<Button
android:id="@+id/btn_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="二、按钮点击事件 实现"
android:textColor="@color/white" />
java代码中点击实现 效果
public class ButtonMethod extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
// 二、按钮点击事件 实现
Button mButton = (Button) findViewById(R.id.btn_test);
BtnClick mBtnClick = new BtnClick();
mButton.setOnClickListener(mBtnClick);
// 二、按钮点击事件 实现
}
// 二、按钮点击事件 实现
class BtnClick implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(ButtonMethod.this, "你点击了按钮点击事件 实现", 1).show();
}
}
// 二、按钮点击事件 实现
}
五、onClick 属性 实现点击事件
xml布局使用
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/custom_btn_white_selector"
android:onClick="BtnTestonClick"
android:text="三、使用 onClick 属性待替 Click 事件"
android:textColor="@color/grey" />
java代码中点击实现 效果
public class ButtonMethod extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
}
// 三、使用 onClick 实现点击事件
public void BtnTestonClick(View view) {
Toast.makeText(this, "你点击了onClick属性按钮", 1).show();
}
// 三、使用 onClick 实现点击事件
}
- 实现效果如下:

至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

Button 使用详解的更多相关文章
- Android零基础入门第19节:Button使用详解
原文:Android零基础入门第19节:Button使用详解 Button(按钮)是Android开发中使用非常频繁的组件,主要是在UI界面上生成一个按钮,该按钮可以供用户单击,当用户单击按钮时,按钮 ...
- Android -- TextView、button方法详解(2)
1. button按下状态的改变 Button bt1 = null; // 声明按钮对象 bt1 = (Button) findViewById(R.id.button1); // 获取按钮对象 b ...
- Android -- TextView、button方法详解(1)
1.TextView常规方法 TextView myTextView=null; //声明变量 myTextView=(TextView)findViewById(R.id.myTextView); ...
- Button、ImageButton及ImageView详解
Button.ImageButton及ImageView详解 在应用程序开发过程中,很多时候需要将View的background或者src属性设置为图片,即美观又支持点击等操作.常见的有Button. ...
- Xamarin.Android通知详解
一.发送通知的机制 在日常的app应用中经常需要使用通知,因为服务.广播后台活动如果有事件需要通知用户,则需要通过通知栏显示,而在Xamarin.Android下的通知需要获取Notification ...
- [转]keil使用详解
第一节 系统概述 Keil C51是美国Keil Software公司出品的51系列兼容单片机C语言软件开发系统,与汇编相比,C语言在功能上.结构性.可读性.可维护性上有明显的优势,因而易学易用.用过 ...
- JavaScript事件详解-jQuery的事件实现(三)
正文 本文所涉及到的jQuery版本是3.1.1,可以在压缩包中找到event模块.该篇算是阅读笔记,jQuery代码太长.... Dean Edward的addEvent.js 相对于zepto的e ...
- 【iOS自定义键盘及键盘切换】详解
[iOS自定义键盘]详解 实现效果展示: 一.实现的协议方法代码 #import <UIKit/UIKit.h> //创建自定义键盘协议 @protocol XFG_KeyBoardDel ...
- iOS开发——高级技术&本地化与国际化详解
本地化与国际化详解 效果如下: 英语: 中文: 具体实现如下: ...
随机推荐
- centos crontab用法详解 定时任务的设置
crontab 是用来让使用者在固定时间或固定间隔执行程序之用,类似于windows的计划任务 安装: yum -y install vixie-cron yum -y install crontab ...
- Codeforces Gym100623J:Just Too Lucky(数位DP)
http://codeforces.com/gym/100623/attachments 题意:问1到n里面有多少个数满足:本身被其各个数位加起来的和整除.例如120 % 3 == 0,111 % 3 ...
- 聚类时的轮廓系数评价和inertia_
在进行聚类分析时,机器学习库中提供了kmeans++算法帮助训练,然而,根据不同的问题,需要寻找不同的超参数,即寻找最佳的K值 最近使用机器学习包里两个内部评价聚类效果的方法:clf=KMeans(n ...
- 2019 Java 全栈工程师进阶路线图,一定要收藏
技术更新日新月异,对于初入职场的同学来说,经常会困惑该往那个方向发展,这一点松哥是深有体会的. 我刚开始学习 Java 那会,最大的问题就是不知道该学什么,以及学习的顺序,我相信这也是很多初学者经常面 ...
- 【深入浅出-JVM】(4):编译 jdk
环境 mac,xcode,jdk8,openjdk,autoconf 步骤 安装autoconf brew install autoconf 下载openjdk源码 git clone https:/ ...
- Spring Boot + Elasticsearch 实现索引的日常维护
全文检索的应用越来越广泛,几乎成了互联网应用的标配,商品搜索.日志分析.历史数据归档等等,各种场景都会涉及到大批量的数据,在全文检索方面,方案无外乎Lucene.Solr.Elasticsearch三 ...
- 三个标签完成springboot定时任务配置
1. 问题描述 Java项目定时任务是必备模块,月高风黑夜跑个批处理,记录或者统计一些系统信息. 2. 解决方案: 结合springboot,只需三个标签就能完成定时任务配置. 2.1 标签1 用在s ...
- ASP.NET CORE配置用户密码验证
在 class Startup 中配置 public void ConfigureServices(IServiceCollection services) { services.AddDbConte ...
- python 3.5学习笔记(第五章)
本章内容 1.什么是模块 2.模块的导入方法 3.搜索路径 4.重要标准库 一.什么是模块 1.模块本质上是一个以.py 结尾的python文件,包含了python对象定义和python语句. 2.模 ...
- xss magic_quotes_gpc
---恢复内容开始--- magic_quotes_gpc函数,在php5.4以上移除了, 但是很奇怪的是 我的5.6版本这边 是可以找到这个选项的. 在php.ini文件里面,默认关闭,如果将此 ...