欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android开发中Button组件的使用

程序员文章站 2022-07-06 16:10:10
前言 安卓系统中,button是程序和用户进行交互的一个重要控件,今天我们就来简单的对button进行学习,其中button组件是文本按钮(继承自textview),而imagebutton是图像按钮...

前言

   安卓系统中,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>

加入之后显示效果如下所示:

Android开发中Button组件的使用 

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();
 }
 });
 }
}

效果如下所示:

Android开发中Button组件的使用 

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;
 }
 }
}

实现效果如下所示:

Android开发中Button组件的使用 

button点击响应说明

上面两种方法是最常用的响应点击事件的方法

到此这篇关于android开发中button组件的使用的文章就介绍到这了,更多相关android中button组件内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!