登录模板
主界面:
<?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">
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
android:padding="10dp">
<TextView
android:id="@+id/TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Launch Your App"
android:textSize="30sp"
android:textStyle="bold"
android:gravity="center"/>
<TextView
android:id="@+id/tv_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="140dp"
android:text=""
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center"/>
<TextView
android:id="@+id/tv_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text=""
android:textSize="17sp"
android:textStyle="bold"
android:gravity="center" />
<TextView
android:id="@+id/tv_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text=""
android:textSize="17sp"
android:textStyle="bold"
android:gravity="center"/>
<Button
android:id="@+id/btn_start_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
android:textStyle="bold"
android:onClick="btn_start_register_onclick"
android:layout_marginTop="30dp"
android:text="register" />
<Button
android:id="@+id/btn_start_signin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
android:textStyle="bold"
android:onClick="btn_start_siginin_onclick"
android:layout_marginTop="10dp"
android:text="sign in" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
注册界面:
<?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=".RegisterActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
android:padding="10dp">
<TextView
android:id="@+id/TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:textSize="22sp"
android:textStyle="bold"
android:gravity="center"/>
<EditText
android:id="@+id/et_reg_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="Email"
android:maxLines="1"
android:inputType="textEmailAddress"
android:text="" />
<EditText
android:id="@+id/et_reg_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="ID"
android:maxLines="1"
android:inputType="textPersonName"
android:text="" />
<EditText
android:id="@+id/et_reg_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Password"
android:maxLines="1"
android:inputType="textPassword" />
<Button
android:id="@+id/btn_reg_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
android:textStyle="bold"
android:layout_marginTop="30dp"
android:onClick="btn_start_register_onclick"
android:text="register" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
登录界面:
<?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=".SigninActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
android:padding="10dp">
<TextView
android:id="@+id/TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sign in"
android:textSize="22sp"
android:textStyle="bold"
android:gravity="center"/>
<EditText
android:id="@+id/et_signin_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="ID"
android:maxLines="1"
android:inputType="textPersonName"
android:text="" />
<EditText
android:id="@+id/et_signin_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Password"
android:maxLines="1"
android:inputType="textPassword" />
<Button
android:id="@+id/btn_signin_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
android:textStyle="bold"
android:onClick="btn_start_siginin_onclick"
android:layout_marginTop="30dp"
android:text="Sign in" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:
package com.example.myexerclse;
import androidx.annotation.Nullable;
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 {
private TextView tvStatus;
private TextView tvId;
private TextView tvEmail;
private static final int REQUEST_REGISTER_CODE=1;
private static final int REQUEST_SIGNIN_CODE=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvStatus=findViewById(R.id.tv_status);
tvId=findViewById(R.id.tv_id);
tvEmail=findViewById(R.id.tv_email);
}
public void btn_start_siginin_onclick(View view) {
Intent intent=new Intent(this,SigninActivity.class);
startActivityForResult(intent,REQUEST_SIGNIN_CODE);
}
public void btn_start_register_onclick(View view) {
Intent intent=new Intent(this,RegisterActivity.class);
startActivityForResult(intent,REQUEST_REGISTER_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if(intent==null){
return;
}
switch (requestCode){
case REQUEST_REGISTER_CODE:
tvStatus.setText("注册成功");
tvId.setText("ID:"+intent.getStringExtra("id"));
tvEmail.setText("Email:"+intent.getStringExtra("email"));
break;
case REQUEST_SIGNIN_CODE:
tvStatus.setText("登录成功");
tvId.setText("您好,"+intent.getStringExtra("id"));
tvEmail.setText("");
break;
}
}
}
注册代码:
package com.example.myexerclse;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
}
public void btn_start_register_onclick(View view) {
EditText etId=findViewById(R.id.et_reg_id);
EditText etEmail=findViewById(R.id.et_reg_email);
EditText etPassword=findViewById(R.id.et_reg_password);
Intent intent=new Intent();
intent.putExtra("id",etId.getText().toString());
intent.putExtra("email",etEmail.getText().toString());
intent.putExtra("password",etPassword.getText().toString());
setResult(1,intent);
finish();
}
}
登录代码:
package com.example.myexerclse;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class SigninActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signin);
}
public void btn_start_siginin_onclick(View view) {
EditText etId=findViewById(R.id.et_signin_id);
EditText etPassword=findViewById(R.id.et_signin_password);
Intent intent=new Intent();
intent.putExtra("id",etId.getText().toString());
intent.putExtra("password",etPassword.getText().toString());
setResult(0,intent);
finish();
}
}
运行截图: