Android中Button组件的使用
前言
安卓系统中,Button是程序和用户进行交互的一个重要控件,今天我们就来简单的对Button进行学习,其中Button组件是文本按钮(继承自TextView),而ImageButton是图像按钮(继承自ImageView)。两者之间的区别在于:
- 1、Button即可显示文本也可显示图形(通过设置背景图),而ImageButton只能显示图形不能显示文本;
- 2、Button可在文本周围区域显示小图,而ImageButton无法在某个区域显示小图;
- 3、ImageButton上的图像可按比例进行拉伸,而Button上的大图会拉伸变形(因为背景图无法按比例拉伸);
从上面可以看出,Button的适应面更广,所以实际开发中基本使用Button。
使用
在界面显示
首先我们能够xml文件中加入Button,如下面代码所示:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".ButtonActivity"><Button
android:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Hello World!"/></android.support.constraint.ConstraintLayout>
加入之后显示效果如下所示:
button说明
就这样,我们就在活动中加入了一个Button控件,并且命名为 Hello World
,但是有没有发现活动上现实的名称和我们输入的名称是不是不一样呢?这是由于系统会对Button控件中所有的英文字母自动进行大写转换,当然,我们肯定需要禁用这一属性,如下面代码,我们进行对这一属性进行禁用
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".ButtonActivity"><Button
android:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Hello World!"android:textAllCaps="false"/></android.support.constraint.ConstraintLayout>
上面代码中,我们使用了android:textAllCaps="false"
进行对默认全部大写进行禁用,当然对于按钮控件不仅仅就这么简单的一些属性,详细信息可通过该文档详细了解。
现在我们的按钮正常显示在活动中,但是我们该怎么让他点击时能够响应,其实响应的方法有很多,下面就来说说常见的两种响应方法
添加响应事件
- 匿名内部类
第一种方法就是在ButtonActivity
中为Button添加监听器,如下面代码所示:
package com.example.jkwu.uicomponent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ButtonActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_button);Button button = findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// 在这里实现响应// 我们在这里就进行ToastToast.makeText(ButtonActivity.this, "点击响应,通过匿名内部类实现", Toast.LENGTH_SHORT).show();}});}
}
效果如下所示:
button点击响应说明
这样,每当点击按钮的时候,就会执行监听器中onClick()方法,我们只需要在这个方法中加入我们需要处理的逻辑就好。
- 实现接口
第二种方法就是使用实现接口的方法进行实现注册监听器的功能,代码如下所示:
package com.example.jkwu.uicomponent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ButtonActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_button);Button button = findViewById(R.id.button);button.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button:// 实现处理逻辑Toast.makeText(ButtonActivity.this, "点击响应,通过实现接口实现", Toast.LENGTH_SHORT).show();break;default:break;}}
}
实现效果如下所示:
button点击响应说明
上面两种方法是最常用的响应点击事件的方法
Android中Button组件的使用相关推荐
- android onclick方法吗,Android中button的onClick事件几种方法
Android中button的onClick事件几种方法 发布时间:2020-09-03 02:36:31 来源:脚本之家 阅读:100 作者:水桶前辈 Android中button的onClick事 ...
- Android中BroadcastReceiver组件详解
Android系统的4个组件终于还剩一种组件了BroadcastReceiver,这个组件是全局监听器,可以监听系统全局的广播消息,可以方便的实现系统中不同组件之间的通信 BroadcastRecei ...
- 【Android】Android中ContentProvider组件详解
原文来自:http://blog.csdn.net/zuolongsnail/article/details/6566317 ContentProvider(内容提供者)是Android中的四大组件之 ...
- Android中设置组件多进程
MultiProcessComponent 源码地址 github源码下载地址https://github.com/onlynight/MultiProcessComponent 概述 这个demo中 ...
- 进阶篇-用户界面:4.Android中常用组件
1.下拉菜单 在Web开发中,HTML提供了下拉列表的实现,就是使用<select>元素实现一个下拉列表,在其中每个下拉列表项使用<option>表示即可.这是在Web开发中一 ...
- android中button点击两次才响应onclick方法
版权声明:本文为博主原创文章,转载请标明出处. https://blog.csdn.net/chaoyu168/article/details/51899718 页面上刚好有EditText 而此 ...
- Android中ContentProvider组件详解
一.Android四大组件 Android四大组件是Activity, Service, Content Provider,Broadcast Receiver. Activity作为程序界面,直接与 ...
- FLASH中button组件的selected和toggle属性解析
在flash中,button组件具有以下属性: 分别为:emphasized.enabled.label.lablePlacement.selected.toggle.visible 下面详细对sel ...
- Android中四大组件
Android四大组件: 一.分类:Activity.Service.Broadcast Receiver.Content Provider. 1.Activity (1)一个Activity通常 ...
最新文章
- 阿里将AI引入时尚界,消费者会对程序员的审美买账吗?
- linux设备驱动学习(三)——并发控制
- wxWidgets:线程间和进程间通信
- C# 序列化理解 1(转)
- Android安全问题 抢先开机启动
- find 命令_教你Linux find命令实例教程:15个find命令用法
- 解决手机QQ发送文件给电脑端,电脑端无法接收的问题
- php mysql begin_PHP mysqli_begin_transaction() 函数用法及示例
- 搜狗批量提交工具(2021)
- 电脑出现无法访问您试图使用的功能所在的网络位置怎么办?(清除软件卸载残留)
- linux进入欢迎界面一直重启,Linux无限重启怎么办
- OSPF ASBR及4类LSA研究
- Git GUI Here 设置成中文界面
- CNSD/Echarts图的使用
- SQL Server—T-SQL编程
- Failed to open \EFI\BOOT\mmx64.efi问题解决
- 我等到花儿也谢 - 张学友 - 真爱
- python快乐编程基础入门,列表与元组
- Spring Cloud Netflix Hystrix
- 【转贴】[原创]V刹安装调整体会