Activity的启动和关闭

1.启动activity

activity的启动分为两种,一种为入口activity,另一种为其他activity

在AndroidManifests进行配置,入口activity的启动只要在要启动的activity里加入intent,例如下面代码将MainActivity作为入口Activity

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

那其他activity如何启动呢?

答案是:startActivity来进行启动,通过intent对象获得要启动的activity,再进行启动。

 psw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,ForgetpswActivity.class);
startActivity(intent);
}
});

2.关闭Activity

直接调用finish()即可关闭当前的活动,需要注意的是,当需要关闭的活动不是主活动而是其他活动时,关闭后就会回到主活动;如果要关闭的活动是主活动

则就直接返回手机主屏幕。

close.setOnClickListener(new View.OnClickListener() {  //为关闭按钮创建监听事件
@Override
public void onClick(View v) { finish(); //关闭当前Activity
}
});

小妙招:刷新当前Activity------》onCreat(null);

Activity启动与关闭的实例:

activity_main.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"
android:background="@drawable/biaoge"
tools:context=".MainActivity"> <TextView
android:id="@+id/zhanghao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:textSize="25sp"
app:layout_constraintBottom_toTopOf="@+id/idpsw"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.145"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.924"
tools:ignore="MissingConstraints" /> <EditText
android:id="@+id/edtzhanghao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="邮箱或者手机号" app:layout_constraintStart_toEndOf="@+id/idpsw"
app:layout_constraintTop_toTopOf="@+id/zhanghao"
tools:ignore="MissingConstraints" /> <TextView
android:id="@+id/idpsw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp" android:text="密码:"
android:textSize="25sp"
app:layout_constraintBaseline_toBaselineOf="@+id/edtpsw"
app:layout_constraintStart_toStartOf="@+id/btnzhuce"
tools:ignore="MissingConstraints" /> <EditText
android:id="@+id/edtpsw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="1dp" android:layout_marginLeft="1dp"
android:layout_marginBottom="205dp"
android:hint="输入6-16位数字或密码"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/idpsw"
tools:ignore="MissingConstraints" /> <Button
android:id="@+id/btnzhuce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:text="注册"
app:layout_constraintBaseline_toBaselineOf="@+id/btnlogin"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints" /> <Button
android:id="@+id/btnlogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:background="#FF8247"
android:text="登录"
app:layout_constraintEnd_toStartOf="@+id/wang_psw"
app:layout_constraintTop_toBottomOf="@+id/edtpsw"
tools:ignore="MissingConstraints" /> <TextView
android:id="@+id/wang_psw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="19dp"
android:layout_marginEnd="76dp"
android:layout_marginRight="76dp"
android:background="#FF8247"
android:text="忘记密码?"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnlogin"
tools:ignore="MissingConstraints" /> </androidx.constraintlayout.widget.ConstraintLayout>

activity_forgetpsw.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"
android:background="@drawable/beijing"
tools:context=".ForgetpswActivity"> <ImageButton
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="16dp"
android:background="#0000"
android:src="@drawable/guanbi"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" /> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="33dp" android:layout_marginLeft="33dp"
android:layout_marginTop="124dp"
android:text="你的邮箱或手机号"
android:textSize="15sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" /> <EditText
android:id="@+id/edtemailorphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="33dp"
android:layout_marginLeft="33dp"
android:layout_marginTop="28dp"
android:hint="请输入手机号或邮箱"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" /> <Button
android:id="@+id/btnok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="56dp"
android:layout_marginLeft="56dp"
android:layout_marginTop="48dp"
android:layout_marginEnd="58dp"
android:layout_marginRight="58dp"
android:background="#fff000"
android:text="确定"
app:layout_constraintEnd_toEndOf="@+id/edtemailorphone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edtemailorphone" /> </androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.forgetpsw;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView psw=(TextView)findViewById(R.id.wang_psw);
psw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,ForgetpswActivity.class);
startActivity(intent);
}
});
}
}

ForgetpswActivity.java

package com.example.forgetpsw;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton; public class ForgetpswActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgetpsw);
ImageButton close = (ImageButton) findViewById(R.id.close); //获取布局文件中的关闭按钮
close.setOnClickListener(new View.OnClickListener() { //为关闭按钮创建监听事件
@Override
public void onClick(View v) { finish(); //关闭当前Activity
}
});
}
}

项目截图:当点击忘记密码会启动忘记密码事件,当点击叉号就会回到主活动。

android之Activity的创建与关闭的更多相关文章

  1. Android课程---Activity的创建

    一.为了创建一个新的Activity,你需要继承Activity类,定义UI,实现功能.新的Activity的最基本的框架代码如下所示: package com.paad.myapplication; ...

  2. Android开发--Activity的创建

    1.Activity概述 Activity是android四大基本组件之一.每一个activity文件对应一个界面,一个程序由多个activity组成. 2.Android工作目录

  3. Android基础Activity篇——创建一个活动(Activity)

    1.创建活动 首先用AS创建一个add no activity项目名使用ActivityTest,包名为默认的com.example.activitytest 2.右击app.java.com.exa ...

  4. Android提升篇系列:Activity recreate(Activity 重新创建/自我恢复)机制(一)

    注:本文中的recreate是指当内存不足时,Activity被回收,但再次来到此Activity时,系统重新恢复的过程.例如:当Activity A到Activity B时,如果内存不足,A被回收, ...

  5. Android开发之Activity的创建跳转及传值

    在Android系统的江湖中有四大组件:活动(Activity), 服务(Service), 广播接收器(Broadcast Reciver)和内容提供者(Content Provider).今天所介 ...

  6. Android 子activity关闭 向父activity传值

    使用startActivity方式启动的Activity和它的父Activity无关,当它关闭时也不会提供任何反馈. 可变通的,你可以启动一个Activity作为子Activity,它与父Activi ...

  7. Android 编程下 Activity 的创建和应用退出时的销毁

    为了确保对应用中 Activity 的创建和销毁状态进行控制,所以就需要一个全局的变量来记录和销毁这些 Activity.这里的大概思路是写一个类继承 Application,并使获取该 Applic ...

  8. Android WiFi开发教程(一)——WiFi热点的创建与关闭

    相对于BlueTooth,WiFi是当今使用最广的一种无线网络传输技术, 几乎所有智能手机.平板电脑和笔记本电脑都支持Wi-Fi上网.因此,掌握基本的WiFI开发技术是非常必要的.本教程将围绕一个小D ...

  9. ADT中创建Android的Activity

    去创建Activity New->Other->Android->Android Activity->BlankActivity: 输入对应的信息: 创建完毕后,可以看到新建了 ...

随机推荐

  1. C# 为什么说事件是一种特殊的委托

    很多人说C#的事件是一种特殊的委托,其实并不是,这是对事件的一种误解 C#  事件模型的五个组成部分 1.事件的拥有者 2.事件成员(事件的本身) 3.事件响应者 4.事件处理器:本质上是一种回调方法 ...

  2. NET Core 3.1 PATCH HTTP 的使用注意事项

    使用Postman请求示例: 一.在Headers要声明请求类型Content-Type 二.body提交要使用raw,且声明为json格式传输 三.如果有authorization验证还需要带上(如 ...

  3. vs2017编译installer项目提示缺少visualstudio2010shell(Integrated)-CHN

    这个问题折腾我一天,欲仙欲死啊~~~~~~! 国内的网站各种百度,都不行. 还是Google大神比较强,在stackoverflow.com找到了解决方法,说来也简单,难者不会,会者不难! ----- ...

  4. vue 源码 学习days8-比较两个对象的方法

    // 在面试中可能会遇到, 思想重要 // 比较两个对象是否是相等的 两个对象 // 1. js 中对象是无法使用 == 来比较的, 比是地址 // 2. 我们一般会定义如果对象的各个属性值都相等 那 ...

  5. Redis远程连接报错解决

    今天测试了一下在本机(win10系统)远程连接 centos下的redis,结果报了以下错误: Exception in thread "main" redis.clients.j ...

  6. 机器学习之路--Numpy

    常用代码 ndarray.dtype 数据类型必须是一样的 常用代码 import numpy #numpy读取文件 world_alcohol = numpy.genfromtxt("wo ...

  7. js中getBoundingClientrRect()方法的详解

    getBoundingClientRect(): 这个方法返回一个矩形对象,包含四个属性:left.top.right和buttom.分别表示元素各边与页面各边的距离 例如: var boxPosit ...

  8. 阿里云ECS服务器Ubuntu配置MySQL远程访问

    root账户登录服务器Ubuntu16.04 apt-get update apt-get install mysql-server mysql-client; 安装时会让你设置root密码,输入2次 ...

  9. Longhorn入门级教程!轻松实现持久化存储!

    介 绍 在本文中你将学会如何使用k3s在Civo上运行Longhorn.如果你还没使用过Civo,可以到官网注册(https://www.civo.com/ )还可以申请免费的使用额度.首先,需要一个 ...

  10. python异常(理论知识)

    异常 程序在运行过程当中,不可避免的会出现一些错误,比如: 使用了没有赋值过的变量 使用了不存在的索引 除0 ... 这些错误在程序中,我们称其为异常. 程序运行过程中,一旦出现异常将会导致程序立即终 ...