首页 > 其他分享 >AndroidStudio学习记录(3):操纵按钮控件Botton、ImageBotton

AndroidStudio学习记录(3):操纵按钮控件Botton、ImageBotton

时间:2024-04-06 14:00:27浏览次数:17  
标签:控件 Botton tv Button AndroidStudio 按钮 msg import android

按钮控件是平时看到的,常用Botton和ImageButton控件,一般操纵 按钮来实现相应的命令,比如在手机上的查找 登录 注册,以及点击命令等等。

ImaBotton与Button的区别在于它没有文本,只有图片,需要制定图片路径

在activity_main.xml文件中,它们是这样使用的:

<?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"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn01"
android:onClick="btn02Click"
android:text="按钮1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn02"
android:text="按钮2"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="100dp"
android:id="@+id/imgBtn"
android:src="@mipmap/ic_launcher_round"
android:contentDescription="TODO" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"/>
</LinearLayout>

接下来是Java后台代码:

package com.aaa.my2;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;


import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.android.material.bottomnavigation.BottomNavigationItemView;

public class MainActivity extends AppCompatActivity {
    //定义变量;ALT+Enter;导入类
    Button btn01,btn02;
    ImageButton imgBtn;
    TextView tv_msg;


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//------------------------------------------------------------------------------
        //获取变量给当前对象
        btn01=(Button) findViewById(R.id.btn01);
        btn02=(Button) findViewById(R.id.btn02);
        imgBtn=(ImageButton) findViewById(R.id.imgBtn);
        tv_msg=(TextView) findViewById(R.id.tv_msg);//

        //事件监听器
        btn01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                tv_msg.setText("这是图片按钮的事件");
            }

        });
        //定义一个方法;
    }
    //自行定义一个方法,这个放在OnCreate外,单独的方法,当单击产生响应
    public void btn02Click(View v) {
        tv_msg.setText(("这个是按钮2自定义的方法"));
    }
}

标签:控件,Botton,tv,Button,AndroidStudio,按钮,msg,import,android
From: https://blog.csdn.net/weixin_53406338/article/details/137427129

相关文章

  • 【Qt】:常用控件(五:显示类控件)
    常用控件一.ProgressBar二.CalendarWidget一.ProgressBar使⽤QProgressBar表⽰⼀个进度条代码⽰例:设置进度条按时间增⻓设置定时器,每个0.1秒,让进度条+1在实际开发中,进度条的取值,往往是根据当前任务的实际进度来进行设置的。比如需要读取一个很大的文......
  • 【Qt】:常用控件(四:显示类控件)
    常用控件一.Lable二.LCDNumber一.LableQLabel可以⽤来显⽰⽂本和图⽚.代码⽰例:显⽰不同格式的⽂本代码⽰例:显⽰图⽚此时,如果拖动窗⼝⼤⼩,可以看到图⽚并不会随着窗⼝⼤⼩的改变⽽同步变化为了解决这个问题,可以在Widget中重写resizeEvent函数。......
  • 第三单元学校里所讲控件
    第三单元学校里所讲控件1.ImageView图片考点1:src和backgroundbackground是背景图片当设置长宽matchparentbackground会铺满而src不会,他会按原图的比例<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android&qu......
  • WPF中Ribbon控件的使用
    WPF中Ribbon控件的使用这篇博客将分享如何在WPF程序中使用Ribbon控件。Ribbon可以很大的提高软件的便捷性。上面截图使Outlook2010的界面,在Home标签页中,将所属的Menu都平铺的布局,非常容易的可以找到想要的Menu。在Outlook2003时代,将Home下面的Menu都垂直的排列下来,操作的便捷程......
  • 【WPF应用34】WPF基本控件-Menu的详解与示例
    WPF(WindowsPresentationFoundation)是.NET框架的一个部分,用于构建桌面应用程序的用户界面。在WPF中,菜单(Menu)是一种常用的控件,用于提供一组选项或命令,使用户可以根据自己的需要执行特定的操作。本文将详细介绍WPF中的Menu控件,包括其基本用法、属性和事件。同时,我们将通过一......
  • 【WPF应用35】深度解析WPF中的TreeView控件:功能、用法、特性与最佳实践
    WPF(WindowsPresentationFoundation)是微软推出的一个用于构建桌面应用程序的图形子系统。在WPF中,TreeView是一种常用的树形控件,用于显示层次结构的数据显示。本文将详细介绍WPF中的TreeView控件,并提供一个简单的示例。一、TreeView控件的基本概念TreeView控件用于显示一......
  • Qt自定义控件之Battery电池控件
    文章目录前言一、BasicBattery二、Battery控件三、效果总结前言在Qt应用程序开发中,自定义控件是一种常见的需求,开发者经常需要根据特定的需求创建定制化的控件来增强用户界面的交互性和美观性。Battery电池控件是一种常见的自定义控件,用于显示设备的电池状态。通过B......
  • playwright for net 对日期选择控件(My97DatePicker)的设置
     playwrightf对日期选择控件的设置,直接使用js脚本publicpartialclassMainForm:Form{IPlaywrightplaywright;IPagepage;publicMainForm(){InitializeComponent();}privateasyncvoidMainForm_Loa......
  • 界面控件Kendo UI for jQuery 2024 Q1亮点 - 新的ToggleButton组件
    Telerik & KendoUI 2024Q1版本于2024年初发布,在此版本中将AI集成到了UI组件中,在整个产品组合中引入AIPrompt组件以及10多个新的UI控件、支持Angular17、多个数据可视化功能增强等。P.S:KendoUIforjQuery提供了在短时间内构建现代Web应用程序所需的一切,从众多UI子控件中......
  • 界面控件Kendo UI for jQuery 2024 Q1亮点 - 新的ToggleButton组件
    Telerik & KendoUI 2024Q1版本于2024年初发布,在此版本中将AI集成到了UI组件中,在整个产品组合中引入AIPrompt组件以及10多个新的UI控件、支持Angular17、多个数据可视化功能增强等。P.S:KendoUIforjQuery提供了在短时间内构建现代Web应用程序所需的一切,从众多UI子控......