Android studio单击按钮时,提示文字信息,并实现图片的显示

2023-10-27

MainActivity2.java

package com.example.myapplication_one;

import android.media.Image;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {
    private EditText editText1;
    private EditText editText2;
    private Button button1, button2;
    ImageView imageView1,imageView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //用于加载页面布局   R.layout.名称
        setContentView(R.layout.layout2);
        init();
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
    }

    private void init() {
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        editText1 = findViewById(R.id.editText1);
        editText2 = findViewById(R.id.editText2);
        imageView1 = findViewById(R.id.imageView1);
        imageView2 = findViewById(R.id.imageView2);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button1:
                //点击button1时的操作
                //context:上下文,text:显示的文本的信息,duration:显示时间
                String name = editText1.getText().toString();
                Toast.makeText(MainActivity2.this, name, Toast.LENGTH_LONG).show();
                imageView1.setImageResource(R.drawable.ic_launcher_background);
                break;
            case R.id.button2:
                Toast.makeText(MainActivity2.this, "点击button按钮2", Toast.LENGTH_LONG).show();
                imageView2.setImageResource(R.drawable.ic_launcher_background);
                break;
            default:
                break;
        }
    }
}

layout2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="24dp"
        android:text="@string/User" />
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_toRightOf="@id/textView1"
        android:hint="请输入用户名"
        android:maxLines="2"
        android:ems="10">
        <requestFocus/>
    </EditText>
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="27dp"
        android:text="@string/Password" />
    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_toRightOf="@id/textView2"
        android:ems="10"
        android:hint="请输入密码"
        android:inputType="textPassword"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="233dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="88dp"
        android:layout_marginTop="46dp"
        android:layout_marginRight="89dp"
        android:textAllCaps="true"
        android:text="登录" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="88dp"
        android:layout_marginTop="270dp"
        android:layout_marginRight="89dp"
        android:background="#ff0000"
        android:gravity="center"
        android:text="TextView"
        android:textColor="#00ff00"
        android:textSize="35sp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="233dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="88dp"
        android:layout_marginTop="100dp"
        android:layout_marginRight="89dp"
        android:textAllCaps="true"
        android:text="注册" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="330dp"
        android:layout_marginRight="89dp"
        app:srcCompat="@mipmap/f1" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/f2"
        android:layout_marginLeft="200dp"
        android:layout_marginTop="330dp"
        android:layout_marginRight="10dp"/>

</RelativeLayout>

说明:
1、 android:background="#ff0000"设置文字的背景颜色
2、android:gravity=“center"表示控件内所有部件整体的位置,注意是相对于控件本身,center就是将其组建内的文字进行了居中
android : gravity=“right/bottom” 组建右下角
3、android:textColor=”#00ff00"设置文字颜色
4、android:textSize="35sp"设置文字大小

strings.xml

<resources>
    <string name="app_name">My Application_one</string>
    <string name="User">用户名</string>
    <string name="Password">密码</string>
</resources>

运行:

补充:
gravity属性的可选值有:

图片来自这里,请点击我

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android studio单击按钮时,提示文字信息,并实现图片的显示 的相关文章

随机推荐

  • fisco-bcos使用caliper进行压力测试

    使用caliper对fisco bcos进行压力测试 通过Caliper进行压力测试程序 注意 官网给出的测试案例会出现错误 我会给出相应的解决方案 本文以centos系统为例进行测试 1 环境要求 第一步 配置基本环境 部署Caliper
  • 勇敢的人

    昨天刚写完做个勇敢的人 发现一篇博客写的非常好 于是果断给它转载一下 这位老师的发言 在某个瞬间打动了现在的我 请观看 https blog csdn net zkl99999 article details 46683535 关键字 毛尖
  • iOS检测网络连接状态

    请从Apple网站下载示例 点此下载 然后将Reachability h 和 Reachability m 加到自己的项目中 并引用 SystemConfiguration framework 就可以使用了 Reachability 中定义
  • Java 23种设计模式的分类和使用场景

    听说过GoF吧 GoF是设计模式的经典名著Design Patterns Elements of Reusable Object Oriented Software 中译本名为 设计模式 可复用面向对象软件的基础 的四位作者 他们分为是 E
  • Qt创建的子线程不断循环,主线程界面一直处于无响应状态

    说明 今天用子线程处理数据 但只创建了子线程 还没有来得及让子线程处理大量的数据 在子线程只作了简单处理 发现主线程界面一直不能响应 在主线程让子线程参数isStop true 也跳不出循环 while isStop emit mySign
  • KCF追踪器在opencv和RM中的应用

    1 理论部分 参考文档 KCF目标跟踪方法分析与总结 概念 1 判别式模型和生成式模型 判别式模型 根据训练数据得到分类函数和分界面 比如说根据SVM模型得到一个分界面 然后直接计算条件概率 P y x 我们将最大的 P y x 生成式模型
  • 模拟登陆 Selenium

    模拟登陆 使用爬虫实现登录操作 为何需要做模拟登陆 有些平台只有登录之后才可以访问其内部其他的子页面 如何实现模拟登陆 模拟点击登录按钮发起的请求即可 阻力 验证码的识别 验证码识别 使用线上的打码平台进行各种各样验证码的识别 不包含滑动验
  • eclipse创建动态web项目

    1 打开eclipse 2 依次选择File new Dynamic Web Project 点击new如果没有Dynamic Web Project 选择Other 3 在wizards下输入web 在下面的选框中选择 Dynamic W
  • 前端(十七)——gitee上开源一个移动端礼盒商城项目(前端+后台)

    博主 小猫娃来啦 文章核心 gitee上开源一个移动端礼盒商城项目 文章目录 前言 开源地址 项目运行命令 项目基本展示 前端效果细节展示视频 前端代码细节展示视频 后台效果展示 后台代码展示 经典优势 思维导图 实现思路 前言 项目样式老
  • 养娃探索记录

    0 3岁 搞好身体 3 6岁 培养好生活习惯 6 9岁 培养好学习习惯 9 12岁 培养自学能力 12 15岁 了解三百六十行 不同行业不同职业都是做什么的 15 18岁 确定未来发展方向和人生目标 自我决定理论对我们的教育有着重要的指导作
  • Anaconda安装Pytorch,以及在Pycharm中的配置

    Anaconda安装Pytorch 以及在Pycharm中的配置 有关Anaconda安装请移步安装Anaconda Python3 9 Tensorflow pytorch cpu 打开Anaconda Prompt 创建环境 这个步骤同
  • 八大排序(二)-----堆排序

    基本思想 1 将带排序的序列构造成一个大顶堆 根据大顶堆的性质 当前堆的根节点 堆顶 就是序列中最大的元素 2 将堆顶元素和最后一个元素交换 然后将剩下的节点重新构造成一个大顶堆 3 重复步骤2 如此反复 从第一次构建大顶堆开始 每一次构建
  • JavaScript 浅层克隆和深度克隆

    文章目录 JS 浅层克隆和深度克隆 1 相关知识点 2 浅层克隆 2 1 浅克隆函数 2 2 运用实例 3 深度克隆 3 1 深克隆步骤分析 3 2 深克隆函数 3 3 运用实例 3 4 hasOwnProperty JS 浅层克隆和深度克
  • 【自学笔记】后端01_Web服务器01_Tomcat

    一 web服务器 web服务器是一个应用程序 对HTTP协议的操作进行封装 使得程序员不必直接对协议进行操作 简化开发 主要功能是 提供网上信息浏览服务 说人话 服务器的作用就是封装了HTTP协议操作简化网站部署 让人可以在浏览器访问部署的
  • 【Leetcode笔记】重复的子字符串

    Leetcode原题链接 重复的子字符串 零 前情提要 自己写了个暴力法 LeetCode的一个巨长的字符串测试 然后嫌弃我时间太长了没通过 先放这里了 自己测试着没什么问题 代码 class Solution def repeatedSu
  • python基础:基本数据类型:整数,浮点数,字符串

    基本数据类型介绍 整数 浮点数 字符串 在介绍之前先来了解一个python在终端窗口运行的解释器 在你安装好python之后 运行终端命令行 输入python后回车 你会看到你的python版本以及提示信息和 gt gt gt 等待输入 你
  • 安卓刷机之pixel

    刷机记录 提示 本例子是pixel sailfish 刷rom 提示 刷rom及刷容比较简单一点 1 首先去谷歌的官网去下载手机对应的机型 2 下载刷机工具platform tools zip github地址好像404了 不过下载的地方很
  • windows 64位 apachect2.4+php5.5 无法加载php_curl模块

    2019独角兽企业重金招聘Python工程师标准 gt gt gt 环境 windows2008 64位 单独装了apachect2 4 php5 5版本 今天服务迁移后 一直报call to undefined function curl
  • unity的触摸类touch使用

    这篇博文将简单的记录 如何用unity处理在移动设备上的触控操作 iOS和Android设备能够支持多点触控 在unity中你可以通过Input touches属性集合访问在最近一帧中触摸在屏幕上的每一根手指的状态数据 简单的触控响应实现起
  • Android studio单击按钮时,提示文字信息,并实现图片的显示

    MainActivity2 java package com example myapplication one import android media Image import android os Bundle import andr