1. 程式人生 > >android之Activity的建立與關閉

android之Activity的建立與關閉

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
            }
        });
    }
}

專案截圖:當點選忘記密碼會啟動忘記密碼事件,當點選叉號就會回到主活動。

 

 

&n