首页 > 其他分享 >初级Android开发学习笔记(三)图形用户界面设计-按钮Button

初级Android开发学习笔记(三)图形用户界面设计-按钮Button

时间:2024-03-16 20:04:12浏览次数:29  
标签:OnClickListener text Button 按钮 import Android button 图形用户界面

 

 

按钮Button及按钮处理事件       

        按钮Button用于处理人机交互的事件,在一般应用程序中常常会用到。由于按钮Button是文本标签TextView的子类,按钮Button继承了文本标签TextView所有的方法和属性。其继承关系如图所示。

4ee40f0c0da04dbabe27d9f2fe459f35.png

       按钮Button在程序设计中最常用的方式是实现OnClickListener监听接口,当单击按钮时,通过OnClickListener监听接口触发onClick()事件,实现用户需要的功能。 OnClickListener接口有一个onClick()方法,在按钮Button实现OnClickListener接口时,一定要重写这个方法。 按钮Button调用OnClickListener接口对象的方法如下:

按钮对象.setOnClickListener(OnClickListener对象);

 

实战测试:

编写程序,当点击按钮命令后,页面标题及文本组件的文字内容发生变化。

相关代码文件:

(1)资源文件strings.xml

(2)布局文件activity_main.xml;

(3)控制文件MainActivity.java;

(1)资源文件strings.xml

<resources>
    <string name="app_name">Liangxiu</string>
    <string name="sentence">Hello,\n Android!</string>
    <string name="sentence_harp">Here is\n Liang Xiu!</string>
    <string name="button_text">点击我</string>
</resources>

(2)布局文件:activity_main.xml

在布局文件中添加一个按钮,其id设置为name_button01。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    android:orientation="vertical"><!--布局内组件摆放方向:垂直-->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sentence"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:textSize="30sp"
        android:textColor="#FFFF0000"
        android:background="@color/black"
        android:id="@+id/name_sentence"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_text"
        android:textSize="20sp"
        android:id="@+id/name_button01"/>

</LinearLayout>

(3)控制文件:  MainActivity.java

在控制文件中,创建一个按钮对象,并且把按钮对象与布局层按键关联起来:findViewById()

调用按键的监听方法,创建一个内部类click:          

按键.setOnClickListener(new click)

内部类click要继承接口View.OnClickListener :

重写click中的onClick()事件,实现当点击按钮时,触发onClick()事件。

package com.example.cst22078;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView text;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text=findViewById(R.id.name_sentence);

        button=findViewById(R.id.name_button01);
        button.setOnClickListener(new click());
    }
    class click implements View.OnClickListener{
        @Override
        public void onClick(View view){
            MainActivity.this.setTitle("改变文本");
            text.setText(R.string.sentence_harp);
            text.setTextSize(30);
            text.setTextColor(Color.BLACK);
            text.setBackgroundColor(0xffff0000);
        }
    }

}

监听函数的另一种调用:

public class MainActivity extends AppCompatActivity {

    TextView text;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text=findViewById(R.id.name_sentence);

        button=findViewById(R.id.name_button01);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                MainActivity.this.setTitle("改变文本");
                text.setText(R.string.sentence_harp);
                text.setTextSize(30);
                text.setTextColor(Color.BLACK);
                text.setBackgroundColor(0xffff0000);
            }
        });
    }
}

最终效果:

点击前:

e5a29eeb2f5045429d6e200580110e8a.png

点击后:

49678110c097446298b51972ad847606.png

相关链接:

初级Android开发学习笔记(一)

​​​​​​初级Android开发学习笔记(二)图形用户界面设计-TextView

 

 

标签:OnClickListener,text,Button,按钮,import,Android,button,图形用户界面
From: https://blog.csdn.net/2401_83587546/article/details/136747352

相关文章

  • 8 个最佳 Android 联系人恢复软件/应用程序 [很少有人知道]
    丢失Android手机上的联系信息可能会令人沮丧,尤其是如果您没有在备份或其他平台上进行备份。但是,不必惊慌,因为有很多适用于Android的联系人恢复软件可用。我们了解找回丢失的联系人的重要性,这就是我们今天测试8个出色的联系人恢复应用程序的原因。好的,在本文中,我们将探讨每......
  • android_get_device_api_level
    {//ret=android_get_device_api_level();ret=28;} {intff_Build_SDK_INT(AVCodecContext*avctx){intret=-1;#if__ANDROID_API__>=24//android_get_device_api_level()isastaticinlinebeforeAPIlevel29.//dlsym()mightdoesn�......
  • 鸿蒙千帆起,Android开发者该如何破局?
    纵观当下的新能源汽车圈、科技圈,华为鸿蒙生态堪称顶流,无论是问界、阿维塔等中高端头部新能源车企与华为鸿蒙的深度合作,还是央视新闻、上海浙江政务民生App启动鸿蒙原生应用开发,整个鸿蒙生态借助华为技术基因、软硬协同等优势,已经深刻影响了科技界的发展。在这样的大背景下,越来......
  • 【Android】使用Android Studio打包APK文件
    文章目录1.新建项目2.打包生成APK3.安装APK 1.新建项目打包APK之前,首先需要新建项目,有基础的可以跳过。无基础的可以参考:使用AndroidStudio运行HelloWorld项目2.打包生成APK1.找到Build->GenerateSignedBundleorAPK->勾选APK  2.首次需要创建......
  • 【Android】使用Apktool反编译Apk文件
    文章目录1.下载Apktool1.1Apktool官网下载1.2百度网盘下载2.安装Apktool3.使用Apktool3.1配置Java环境3.2准备Apk文件3.3反编译Apk文件3.3.1解包Apk文件3.3.2修改Apk文件3.3.3打包Apk文件3.3.4签名Apk文件 1.下载Apktool要使用Apkto......
  • 【Android】使用Android Studio运行Hello World项目
    文章目录1.JDK的安装与配置2.AndroidStudio的安装3.运行HelloWorld项目3.1新建项目3.2修改项目配置3.2.1修改UI界面3.2.2配置AndroidSDK3.3添加并运行虚拟设备3.4运行项目 1.JDK的安装与配置想要使用AndroidStudio,必须先配置Java环境,需要......
  • Android混淆后的bug日志通过mapping文件找对应行号
    背景由于项目中提测以及线上的apk都是经过混淆处理的,因此拿到日志后也无法正常查看崩溃日志的行号这个原因是因为混淆了文件,输出的日志是对应不上源文件的,为了正确找到行号需要用到mapping.txt文件配置开启保留行号和源文件要想利用mapping文件找到对应的行号,则还需要在混淆......
  • Qt QToolButton设置icon与字体居中
    目的设置QToolButtonicon与字体居中显示,网上有挺多文章说明的,有些我设置了无效,我亲自测试了下,如下是有效果的1QToolButton{2color:#b9b9b9;3font-size:12px;4font-weight:normal;5border:2pxsolid#b9b9b9;6border-ra......
  • cdkdroplist,调整button排序和位置问题
    buttonenhancement;简单做个记录,给自己看的哈哈!!!应用场景由于cdkdroplist多行时的item移动难用的一批,跟需求讨论决定搞成单行的滚动条;好了开干,玩的就是真实。item少的时候就不说了,正常。当item多时需要滚动,开始操作:超出文本滚动,不换行...等等,原本根据配置的居左居右咋办。原......
  • Android 辅助功能 -抢红包(三)
    Android辅助功能-抢红包(三)本篇文章继续讲述辅助功能.主要通过监听通知栏红包消息,来跳转聊天页面,并自动回复对方"谢谢".上篇文章我们讲述了监听notification,跳转聊天界面.具体可查看:Android辅助功能-抢红包(二)1:使用monitor抓取id.打开androiddevicemonitor......