Android-第四节01Activity(基础知识)(1)

2.匿名内部类

方法一:

在这里插入图片描述

代码:

Button button=findViewById(R.id.button);

class MyClick implements View.OnClickListener{

@Override

public void onClick(View v) {

Log.i(“biaoqian”,“点击了一下按钮”);

}

}

button.setOnClickListener(new MyClick());

方法二:

在这里插入图片描述

代码:

Button button=findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Log.i(“niming”,“第二次点击按钮”);

}

});

3.获取文本值并判断正误


Toast.makeText()各个参数都是干嘛的

Toast:是一个类,主要管理消息的提示。

makeText(),是Toast的一个方法,用来显示信息,分别有三个参数。

第一个参数:this,是上下文参数,指当前页面显示

第二个参数:“string ”是你想要显示的内容

第三个参数:

Toast.LENGTH_LONG,指显示消息时间长短,另一个是ToastLENGTH_SHORT,大概2秒钟。

show(),表示显示这个Toast消息提醒,当程序运行到这里的时候,就会显示出来,如果不调用show()方法,这个Toast对象存在,但是并不会显示,所以一定不要忘记。

在这里插入图片描述

代码:

package com.hnucm.a_test04;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

TextView textView; //定义textView组件

ImageView imageView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

EditText editText1=findViewById(R.id.editText1);

EditText editText2=findViewById(R.id.editText2);

//通过findViewById

textView=findViewById(R.id.textView1);

textView.setText(“这是修改逻辑代码!”);

textView.setTextSize(20);

textView.setTextColor(getResources().getColor(R.color.black));

imageView=findViewById(R.id.imageView1);

imageView.setImageDrawable(getResources().getDrawable(R.drawable.dzq));

Button button=findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String username=editText1.getText().toString();

String password=editText2.getText().toString();

if (username.equals(“hnucm”)&&password.equals(“123456”)){

Toast.makeText(MainActivity.this,“登录成功”,Toast.LENGTH_LONG).show();

}

else{

Toast.makeText(MainActivity.this,“登录失败”,Toast.LENGTH_LONG).show();

}

}

});

}

}

4.简单加法计算器


在这里插入图片描述

xml代码:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.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=“.MainActivity”>

<TextView

android:id=“@+id/textView1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“Hello World!”

android:textSize=“50sp”

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintLeft_toLeftOf=“parent”

app:layout_constraintRight_toRightOf=“parent”

app:layout_constraintTop_toTopOf=“parent” />

<ImageView

android:id=“@+id/imageView1”

android:layout_width=“80dp”

android:layout_height=“80dp”

android:layout_marginStart=“32dp”

android:layout_marginTop=“28dp”

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintTop_toTopOf=“parent”

tools:srcCompat=“@tools:sample/avatars” />

<Button

android:id=“@+id/button”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginTop=“88dp”

android:text=“按钮”

app:layout_constraintEnd_toEndOf=“parent”

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintTop_toBottomOf=“@+id/textView1” />

<EditText

android:id=“@+id/editText1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginTop=“24dp”

android:hint=“请输入账号”

app:layout_constraintEnd_toEndOf=“parent”

app:layout_constraintHorizontal_bias=“0.498”

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintTop_toBottomOf=“@+id/imageView1” />

<EditText

android:id=“@+id/editText2”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:hint=“请输入密码”

app:layout_constraintEnd_toEndOf=“parent”

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintTop_toBottomOf=“@+id/editText1” />

<TextView

android:id=“@+id/textView”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginEnd=“20dp”

android:layout_marginBottom=“4dp”

android:text=“+”

android:textSize=“30dp”

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintEnd_toStartOf=“@+id/editText4” />

<Button

android:id=“@+id/button2”

android:layout_width=“50dp”

android:layout_height=“wrap_content”

android:layout_marginEnd=“156dp”

android:text=“=”

android:textSize=“30dp”

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintEnd_toEndOf=“parent” />

<EditText

android:id=“@+id/editText3”

android:layout_width=“50sp”

android:layout_height=“wrap_content”

app:layout_constraintBottom_toBottomOf=“parent”

app:layout_constraintEnd_toStartOf=“@+id/textView”

app:layout_constraintHorizontal_bias=“0.403”

app:layout_constraintStart_toStartOf=“parent” />

<EditText

android:id=“@+id/editText4”

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

-K9OQNZib-1715021176338)]

[外链图片转存中…(img-sfB5vgG5-1715021176339)]

[外链图片转存中…(img-vlvkz70c-1715021176339)]

[外链图片转存中…(img-AJimO54w-1715021176339)]

[外链图片转存中…(img-5JuAzUbM-1715021176340)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

  • 29
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值