Android中Button组件的使用

一时失言乱红尘 2022-05-17 03:50 256阅读 0赞

前言

安卓系统中,Button是程序和用户进行交互的一个重要控件,今天我们就来简单的对Button进行学习,其中Button组件是文本按钮(继承自TextView),而ImageButton是图像按钮(继承自ImageView)。两者之间的区别在于:

  • 1、Button即可显示文本也可显示图形(通过设置背景图),而ImageButton只能显示图形不能显示文本;
  • 2、Button可在文本周围区域显示小图,而ImageButton无法在某个区域显示小图;
  • 3、ImageButton上的图像可按比例进行拉伸,而Button上的大图会拉伸变形(因为背景图无法按比例拉伸);
    从上面可以看出,Button的适应面更广,所以实际开发中基本使用Button。

使用

在界面显示

首先我们能够xml文件中加入Button,如下面代码所示:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <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">
  3. <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!" />
  4. </android.support.constraint.ConstraintLayout>

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

70
button说明

就这样,我们就在活动中加入了一个Button控件,并且命名为 Hello World,但是有没有发现活动上现实的名称和我们输入的名称是不是不一样呢?这是由于系统会对Button控件中所有的英文字母自动进行大写转换,当然,我们肯定需要禁用这一属性,如下面代码,我们进行对这一属性进行禁用

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <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">
  3. <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!" android:textAllCaps="false" />
  4. </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 {

    1. @Override
    2. protected void onCreate(Bundle savedInstanceState) {
    3. super.onCreate(savedInstanceState);
    4. setContentView(R.layout.activity_button);
    5. Button button = findViewById(R.id.button);
    6. button.setOnClickListener(new View.OnClickListener() {
    7. @Override
    8. public void onClick(View v) {
    9. // 在这里实现响应
    10. // 我们在这里就进行Toast
    11. Toast.makeText(ButtonActivity.this, "点击响应,通过匿名内部类实现", Toast.LENGTH_SHORT).show();
    12. }
    13. });
    14. }

    }

效果如下所示:

70 1
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 {

    1. @Override
    2. protected void onCreate(Bundle savedInstanceState) {
    3. super.onCreate(savedInstanceState);
    4. setContentView(R.layout.activity_button);
    5. Button button = findViewById(R.id.button);
    6. button.setOnClickListener(this);
    7. }
    8. @Override
    9. public void onClick(View v) {
    10. switch (v.getId()) {
    11. case R.id.button:
    12. // 实现处理逻辑
    13. Toast.makeText(ButtonActivity.this, "点击响应,通过实现接口实现", Toast.LENGTH_SHORT).show();
    14. break;
    15. default:
    16. break;
    17. }
    18. }

    }

实现效果如下所示:

70 2
button点击响应说明


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

发表评论

表情:
评论列表 (有 0 条评论,256人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android Button:按钮控

    按钮(Button)应该是用户交互中使用最多的组件,在很多应用程序中都很常见。当用户单击按钮的时候,会有相对应的响应动作。 下面在 WidgetDemo 工程的主界面