Android开发中Button组件的使用
作者:suwu150 时间:2021-07-08 07:18:13
前言
安卓系统中,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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 在这里实现响应
// 我们在这里就进行Toast
Toast.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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
Button button = findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
// 实现处理逻辑
Toast.makeText(ButtonActivity.this, "点击响应,通过实现接口实现", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
实现效果如下所示:
button点击响应说明
上面两种方法是最常用的响应点击事件的方法
来源:https://blog.csdn.net/suwu150/article/details/81452412
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android RichText 让Textview轻松的支持富文本(图像ImageSpan、点击效果等等类似QQ微信聊天)
深入了解Spring中最常用的11个扩展点
![](https://img.aspxhome.com/file/2023/1/57531_0s.png)
C/C++中比较字符串的方法详解
![](https://img.aspxhome.com/file/2023/5/109775_0s.png)
IDEA中Spring项目的工程构建
![](https://img.aspxhome.com/file/2023/6/72536_0s.jpg)
Maven配置文件pom.xml详解
Android中ListView的几种常见的优化方法总结
![](https://img.aspxhome.com/file/2023/9/116009_0s.png)
Android studio实现画板功能
![](https://img.aspxhome.com/file/2023/2/137172_0s.png)
Android 将view 转换为Bitmap出现空指针问题解决办法
IntelliJ IDEA如何设置JDK版本
![](https://img.aspxhome.com/file/2023/7/126337_0s.png)
SpringBoot配置及使用Schedule过程解析
浅试仿 mapstruct实现微服务编排框架详解
![](https://img.aspxhome.com/file/2023/0/63420_0s.png)
java在linux本地执行shell命令的实现方法
![](https://img.aspxhome.com/file/2023/9/59569_0s.png)
基于Avalonia实现自定义弹窗的示例详解
![](https://img.aspxhome.com/file/2023/0/98290_0s.png)
实例讲解Java中动态代理和反射机制
基于android中权限的集合汇总
Java去重排序之Comparable与Comparator的使用及说明
![](https://img.aspxhome.com/file/2023/8/122278_0s.png)
java实现分页显示效果
![](https://img.aspxhome.com/file/2023/8/61378_0s.jpg)
一起来学习Java IO的转化流
![](https://img.aspxhome.com/file/2023/2/72612_0s.png)
Android破解微信获取聊天记录和通讯录信息(静态方式)
![](https://img.aspxhome.com/file/2023/3/118963_0s.png)