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开发——高级技术&本地化与国际化详解
本地化与国际化详解 效果如下: 英语: 中文: 具体实现如下: ...
随机推荐
- Ruby中的各种比较方式对比
Ruby中设计了很多种比较方式,所有对象都能进行==.!=.===.<=>.eql?.equal?这几种比较.此外,当实现了<=>之后,如果还include了Comparabl ...
- 找不到 main 方法
前几天打开了好久不用的eclipse,发现报了个奇怪的错误 ***中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args) ...
- Smobiler控件的使用:ListView的数据绑定及实现多选
环境 SmobilerDesigner 4.7 Visual Studio 2010以上 正文 listview绑定数据 打开Visual Studio ,新建一个SmobilerApplicatio ...
- JavaScript 之迭代方法
前言:关于 JS 中为数组定义的迭代方法,我最开始是在<JavaScript高级程序设计>中学习的,然后...我并没有看懂,后来翻阅各个大佬的博客,稍微理解了那么一丢丢.以下就是我的一点见 ...
- Docker笔记(三):Docker安装与配置
原文地址:http://blog.jboost.cn/2019/07/14/docker-3.html Docker分为Docker CE社区免费版与Docker EE企业收费版.Docker EE主 ...
- JDK1.8--体验Stream表达式,从一个对象集合中获取每一个对象的某一个值返回新集合
xl_echo编辑整理,欢迎转载,转载请声明文章来源.更多IT.编程案例.资料请联系QQ:1280023003 百战不败,依不自称常胜,百败不颓,依能奋力前行.——这才是真正的堪称强大!! --- 开 ...
- vector是序列式容器而set是关联式容器。set包含0个或多个不重复不排序的元素。
1.vector是序列式容器而set是关联式容器.set包含0个或多个不重复不排序的元素.也就是说set能够保证它里面所有的元素都是不重复的.另外对set容器进行插入时可以指定插入位置或者不指定插入位 ...
- 记录一次pycharm中,引入其他类可用,下面总是有波浪线,而且Ctrl+b 无法查看类函数的源码
最近在玩python,发现引入其他的函数们总是有波浪线,但是能够使用,crtl+b却无法看到,非常尴尬,然后查看了原因,记录如下: This inspection detects names that ...
- 洛谷P2299 Mzc和体委的争夺战 题解
题目 题目描述 mzc家很有钱(开玩笑),他家有n个男家丁(做过前三弹的都知道).但如此之多的男家丁吸引来了我们的体委(矮胖小伙),他要来与mzc争夺男家丁. mzc很生气,决定与其决斗,但cat的体 ...
- 个人永久性免费-Excel催化剂功能第104波-批量选择多种类型的图形对象
在Excel的日常操作过程中,选择绝对是一个高频的操作,之前开发过一些快速选择单元格区域的辅助功能,除了单元格区域,Excel强大之处在于,类似PhotoShop那般可以存放多种图形,并且有图层先后顺 ...