标签:otto back traints drawable rri tla const tools oba
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 } }); } }
项目截图:当点击忘记密码会启动忘记密码事件,当点击叉号就会回到主活动。
标签:otto back traints drawable rri tla const tools oba
原文地址:https://www.cnblogs.com/xiaofengzai/p/12271526.html