登录注册实现
登录注册实现
1.Splash页面
(1)保存一张图片到MyApplication\app\src\main\res\drawable;
(2)新建Splash活动,New->Activity->Empty Activity,命名为“Splash”;
(3)在 layout/ activity_splash.xml中,调整布局,添加 ImageView组件用于放置启动页显示的图片,xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".Splash"
android:background="@drawable/bc"/>
2.Login页面
(1)新建Login活动,New->Activity->Empty Activity,命名为“Login”;
(2)修改Login页面布局:

(3)对应的activity_login代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".Login"
android:background="@drawable/t">
<TextView
android:id="@+id/textView"
android:layout_width="409dp"
android:layout_height="198dp"
android:gravity="center"
android:text="@string/textView"
android:textColor="@color/blue"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="183dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="101dp"
android:orientation="horizontal">
<TextView
android:id="@+id/TextView1"
android:layout_width="104dp"
android:layout_height="53dp"
android:gravity="center"
android:text="@string/textView1"
android:textColor="@color/hotpink"
android:textSize="20sp" />
<EditText
android:id="@+id/Editext1"
android:layout_width="match_parent"
android:layout_height="53dp"
android:background="@drawable/shape_round"
android:hint="@string/Editext1"
android:textColor="@color/plum"
android:textColorHint="@color/blue" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="53dp">
<TextView
android:id="@+id/TextView2"
android:layout_width="106dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/textView2"
android:textColor="@color/hotpink"
android:textSize="20sp" />
<EditText
android:id="@+id/Editext2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/plum"
android:inputType="number"
android:textColorHint="@color/blue"
android:background="@drawable/shape_round"
android:hint="@string/Editext2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="129dp"
android:layout_marginTop="20dp"
android:orientation="vertical">
<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="140dp"
android:background="@drawable/shape_round"
android:text="@string/Button1"
android:textColor="@color/plum"
android:textSize="20sp" />
<Button
android:id="@+id/Button2"
android:layout_width="239dp"
android:layout_height="64dp"
android:layout_marginLeft="76dp"
android:background="@null"
android:paddingTop="20dp"
android:text="注册请点击这里哦"
android:textColor="@color/purple_200"
android:textColorHint="@color/purple_500"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
3.Register页面
(1)新建Register活动,New->Activity->Empty Activity,命名为“Register”;
(2)修改Register页面布局:

(3)对应的registe_login代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".Register"
android:background="@drawable/h">
<TextView
android:id="@+id/TextView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="10sp"
android:text="@string/textView3"
android:textColor="@color/pink3"
android:textSize="40sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="83dp">
<TextView
android:id="@+id/TextureView4"
android:layout_width="116dp"
android:layout_height="60dp"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="@string/textView1"
android:textColor="@color/plum"
android:textSize="20sp" />
<EditText
android:id="@+id/Editext3"
android:layout_width="231dp"
android:layout_height="48dp"
android:layout_marginTop="16dp"
android:background="@drawable/shape_round"
android:hint="@string/Editext1"
android:textColorHint="@color/blue"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="73dp">
<TextView
android:id="@+id/TextView6"
android:layout_width="118dp"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="@string/textView2"
android:textColor="@color/plum"
android:textSize="20sp" />
<EditText
android:id="@+id/Editext4"
android:layout_width="231dp"
android:layout_height="48dp"
android:layout_marginTop="10dp"
android:background="@drawable/shape_round"
android:hint="@string/Editext2"
android:textColorHint="@color/blue"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="73dp">
<TextView
android:id="@+id/TextView2"
android:layout_width="118dp"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="@string/textView2"
android:textColor="@color/plum"
android:textSize="20sp" />
<EditText
android:id="@+id/Editext5"
android:layout_width="231dp"
android:layout_height="48dp"
android:layout_marginTop="10dp"
android:background="@drawable/shape_round"
android:hint="请再次确认密码"
android:textColorHint="@color/blue"
android:textSize="18sp" />
</LinearLayout>
<Button
android:id="@+id/Button3"
android:layout_width="120dp"
android:layout_height="70dp"
android:layout_marginLeft="150dp"
android:background="@drawable/shape_round"
android:hint="@string/Button3"
android:textColor="@color/blue"
android:textColorHint="@color/plum"
android:layout_marginTop="220dp"
android:textSize="20sp" />
</LinearLayout>
4.Main页面
(1)修改Main页面布局:

(2)对应的main_login代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:background="@color/blue"
tools:context=".Main" >
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="74dp"
android:gravity="center"
android:textSize="35sp"
android:textColor="@color/hotpink"
android:text="这里是学习乐园" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="440dp"
android:layout_height="283dp"
app:srcCompat="@drawable/s" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/u" />
</LinearLayout>
5. 修改Splash.java,实现页面定时跳转
在Splash.java文件的 onCreate()方法中,增加以下代码:
//增加定时器,实现页面跳转
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent mainIntent = new Intent(Splash.this, Login.class);
startActivity(mainIntent);
finish();
}
}, 3000);
修改后如下所示:
package com.example.myapplication2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
public class Splash extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//去标题,设置全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//增加定时器,实现页面跳转。
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent mainIntent = new Intent(Splash.this, Login.class);
startActivity(mainIntent);
finish();
}
}, 3000);
/*在 onCreate()方法中,通过 postDelayed方法调用了一个新定义的 Runnable对象,该调用在 3000ms后生效,相当于一个定时器效果。新定义的 Runnable对象用以实现页面跳转*/
}
}
6.修改程序入口活动页为 Splash,将 AndroidManifest.xml中内容修改为如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication2" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication2" >
<!-- 修改程序入口 -->
<activity
android:name=".Splash"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 修改程序入口 -->
<activity
android:name=".Register"
android:exported="true" />
<activity
android:name=".Main"
android:exported="true" />
<activity
android:name=".Login"
android:exported="true" />
</application>
</manifest>
7.修改 Loign.java,为登录和注册按钮设置事件监听处理。
(1)在Loign.java的class代码中定义私有变量
private Button button1,button2;
private EditText text1,text2;
//需要维护两个全局私有变量,用来存储用户名和密码。
private String login_name="yy";// 保存的用户名、或注册的新用户名
private String login_pss = "121";// 保存的密码、或注册的密码
private String s1,s2;//用户输入的用户名和密码
(2)在onCreate方法中绑定监听“登录”按钮,并注册监听
//绑定对应的按钮
button1 = (Button) findViewById(R.id.Button1);
button2 = (Button) findViewById(R.id.Button2);
text1 = (EditText) findViewById(R.id.Editext1);
text2 = (EditText) findViewById(R.id.Editext2);
//注册监听登录按钮
button1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
(3)在onClick方法中,获取并匹配信息
s1 = text1.getText().toString(); //获取输入的用户名
s2 = text2.getText().toString(); //获取输入的密码
// 如果获取文本长度为0,创建一个信息对话框,弹出“登录信息不可为空”
if (s1.length()==0 || s2.length()==0)
{
new AlertDialog.Builder(Login.this).setTitle("登录信息不可为空")
.setMessage("请输入用户名或者密码")
.setPositiveButton("确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
//finish();
}
}).show();
text1.setText("");//清空用户框
text2.setText("");//清空密码框
text1.requestFocus();//获取焦点,光标出现
}
//输入的信息与预设用户名、密码不匹配,弹出对话框提示“账号或密码错误”
else if (!s1.equals(login_name) || !s2.equals(login_pss))
{
new AlertDialog.Builder(Login.this).setTitle("登录信息有误")
.setMessage("用户名或者密码错误")
.setPositiveButton("确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
//finish();
}
}).show();
text1.setText("");//清空用户框
text2.setText("");//清空密码框
text1.requestFocus();//获取焦点,光标出现
}
//如果用户名和密码匹配成功,则Toast 显示出用户名和密码,跳转主页面
else {
String msg = "欢迎进入学习乐园吖 \n 您输入的用户名是:"+ login_name + "\n 密码是: "+login_pss ;
Toast.makeText(Login.this,msg,Toast.LENGTH_LONG).show();
Intent intent = new Intent(Login.this, Main.class);//创建intent对象,实现页面跳转
startActivity(intent);//直接跳转,无信息交换
text1.setText("");//清空用户框
text2.setText("");//清空密码框
text1.requestFocus();//获取焦点,光标出现
}
}
});
(3)在Loign.java的onCreate方法中绑定、监听“注册”按钮
button2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent intent = new Intent(Login.this,Register.class);
startActivityForResult(intent,1);//携带信息跳转,信息码1
}
});
(4)在Loign.java的class中,以startActivityForResult的形式启动 Register活动,接收其注册成功后返回的用户名和密码信息。页面跳转的同时,携带请求码,重写onActivityResult方法,对于注册界面返回不同结果码。
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//在一个活动中有可能调用 startActivityForResult()方法去启动很多不同的活动,这里的请求码对应1
if (requestCode == 1)
{
//当Login中的Button被触发 onClick() 事件后,把Register中EditText的内容返回给Login。
//返回到Login后,执行onActivityResult()方法,判断结果码无误后,获取Register返回的结果,把Login中的EditText的内容 改为Register返回的结果。
//通过这样的方式打开这两个Activity,使他们中EditText的内容同步,一个 Activity 改变了,到另一个 Activity 中也会跟着改变。
if (resultCode == RESULT_OK)
{
//注册成功,弹出提示框
new AlertDialog.Builder(Login.this).setTitle("注册成功")
.setMessage("成功注册账号,已自动返回登录页面。")
.setPositiveButton("确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
//finish();
}
}).show();
login_name = data.getStringExtra("regName");
login_pss = data.getStringExtra("regPwd");
}
(5)修改后的完整Loign.java代码:
package com.example.myapplication2;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends AppCompatActivity {
private Button button1,button2;
private EditText text1,text2;
//需要维护两个全局私有变量,用来存储用户名和密码。
private String login_name="yy";// 保存的用户名、或注册的新用户名
private String login_pss = "121";// 保存的密码、或注册的密码
private String s1,s2;//用户输入的用户名和密码
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//绑定对应的按钮
button1 = (Button) findViewById(R.id.Button1);
button2 = (Button) findViewById(R.id.Button2);
text1 = (EditText) findViewById(R.id.Editext1);
text2 = (EditText) findViewById(R.id.Editext2);
//注册监听登录按钮
button1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View view)
{
s1 = text1.getText().toString(); //获取输入的用户名
s2 = text2.getText().toString(); //获取输入的密码
// 如果获取文本长度为0,创建一个信息对话框,弹出“登录信息不可为空”
if (s1.length()==0 || s2.length()==0)
{
new AlertDialog.Builder(Login.this).setTitle("登录信息不可为空")
.setMessage("请输入用户名或者密码")
.setPositiveButton("确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
//finish();
}
}).show();
text1.setText("");//清空用户框
text2.setText("");//清空密码框
text1.requestFocus();//获取焦点,光标出现
}
//输入的信息与预设用户名、密码不匹配,弹出对话框提示“账号或密码错误”
else if (!s1.equals(login_name) || !s2.equals(login_pss))
{
new AlertDialog.Builder(Login.this).setTitle("登录信息有误")
.setMessage("用户名或者密码错误")
.setPositiveButton("确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
//finish();
}
}).show();
text1.setText("");//清空用户框
text2.setText("");//清空密码框
text1.requestFocus();//获取焦点,光标出现
}
//如果用户名和密码匹配成功,则Toast 显示出用户名和密码,跳转主页面
else {
String msg = "欢迎进入学习乐园吖 \n 您输入的用户名是:"+ login_name + "\n 密码是: "+login_pss ;
Toast.makeText(Login.this,msg,Toast.LENGTH_LONG).show();
Intent intent = new Intent(Login.this, Main.class);//创建intent对象,实现页面跳转
startActivity(intent);//直接跳转,无信息交换
text1.setText("");//清空用户框
text2.setText("");//清空密码框
text1.requestFocus();//获取焦点,光标出现
}
}
});
//绑定、监听“注册”按钮
button2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent intent = new Intent(Login.this,Register.class);
startActivityForResult(intent,1);//携带信息跳转,请求码1,代表Register页面
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//在一个活动中有可能调用 startActivityForResult()方法去启动很多不同的活动
if (requestCode == 1)
{
//当Login中的Button被触发 onClick() 事件后,把Register中EditText的内容返回给Login。
//返回到Login后,执行ononActivityResult()方法,判断RequestCode和ResultCode无误后,把Login中的EditText的内容改为Register返回的结果。
//通过这样的方式打开这两个Activity,使他们中EditText的内容同步,一个 Activity 改变了,到另一个 Activity 中也会跟着改变。
if (resultCode == RESULT_OK)
{
//注册成功,弹出提示框
new AlertDialog.Builder(Login.this).setTitle("注册成功")
.setMessage("成功注册账号,已自动返回登录页面。")
.setPositiveButton("确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
//finish();
}
}).show();
login_name = data.getStringExtra("regName");
login_pss = data.getStringExtra("regPwd");
}
}
}
}
8.修改 Register.java,为注册活动的注册按钮设置事件监听处理程序,单击按钮后检查用户输入的基本信息。
(1)在 Register.java的class代码中定义私有变量
public class Register extends AppCompatActivity {
private EditText text1,text2,text3;
private Button btn1;
private String s1,s2,s3;//用户注册时输入的新用户名、新密码和确认密码
(2)在onCreate方法中绑定监听“登录”按钮,并注册监听
//利用ID绑定文本框和按钮
text1 = (EditText)findViewById(R.id.Editext3);
text2 = (EditText)findViewById(R.id.Editext4);
text3 = (EditText)findViewById(R.id.Editext5);
btn1 =(Button)findViewById(R.id.Button3);
//注册监听
btn1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v)
{
(3)在onClick方法中,获取并匹配信息,若注册成功则跳转Login页面
s1 = text1.getText().toString(); //获取注册时输入的新用户名
s2 = text2.getText().toString(); //获取注册时输入的新密码
s3 = text3.getText().toString(); //获取注册时输入的新密码
// 如果两次输入的密码不一致,则提示注册失败
if (!s2.equals(s3))
{
new AlertDialog.Builder(Register.this).setTitle("注册失败")
.setMessage("请确认两次输入的密码一致")
.setPositiveButton("重新注册", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
//finish();
}
}).show();
text1.setText("");//清空用户框
text2.setText("");//清空密码框
text3.setText("");//清空密码框
text1.requestFocus();//获取焦点,光标出现
}
//如果注册成功,则返回登录页面
else
{
Intent intent2=new Intent(); //intent没有参数,仅仅只传递数据
//将用户名和密码通过 putExtra()方法放入intent中,传递给Login页面
intent2.putExtra("regName",text1.getText().toString());
intent2.putExtra("regPwd",text2.getText().toString());
//在子活动中调用setResult方法,设置返回结果 intent2,并且其中包含传递的参数
setResult(RESULT_OK, intent2);
finish();//关闭当前页面
}
}
});
}
}
(4)完整的Register.java代码:
package com.example.myapplication2;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Register extends AppCompatActivity {
private EditText text1,text2,text3;
private Button btn1;
private String s1,s2,s3;//用户注册时输入的新用户名、新密码和确认密码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//利用ID绑定文本框和按钮
text1 = (EditText)findViewById(R.id.Editext3);
text2 = (EditText)findViewById(R.id.Editext4);
text3 = (EditText)findViewById(R.id.Editext5);
btn1 =(Button)findViewById(R.id.Button3);
//注册监听
btn1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v)
{
s1 = text1.getText().toString(); //获取注册时输入的新用户名
s2 = text2.getText().toString(); //获取注册时输入的新密码
s3 = text3.getText().toString(); //获取注册时输入的新密码
// 如果两次输入的密码不一致,则提示注册失败
if (!s2.equals(s3))
{
new AlertDialog.Builder(Register.this).setTitle("注册失败")
.setMessage("请确认两次输入的密码一致")
.setPositiveButton("重新注册", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
//finish();
}
}).show();
text1.setText("");//清空用户框
text2.setText("");//清空密码框
text3.setText("");//清空密码框
text1.requestFocus();//获取焦点,光标出现
}
//如果注册成功,则返回登录页面
else
{
Intent intent2=new Intent(); //intent没有参数,仅仅只传递数据
//将用户名和密码通过 putExtra()方法放入intent中,传递给Login页面
intent2.putExtra("regName",text1.getText().toString());
intent2.putExtra("regPwd",text2.getText().toString());
//在子活动中调用setResult方法,设置返回结果 intent2,并且其中包含传递的参数
setResult(RESULT_OK, intent2);
finish();//关闭当前页面
}
}
});
}
}
9.去标题
(1)Splash.java中加入以下代码:
//设置全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
(2)在res/themes文件中,修改主题


运行结果
1.动态效果

2.静态效果
(1).Splash页面
启动app后,定时器计时3000ms的动画效果,并去除标题栏

(2).Login页面
①登录页初始界面

②输入空的用户名、密码,弹出提示框,提示“请输入用户名和密码”


③输入错误的用户名、密码,弹出提示框,提示“用户名或密码不正确”


④输入正确的用户名(默认为yy)、密码(默认121),登录成功,转入Main页面,长时间Toast显示登录信息

(3).Register页面
①点击Login页面的注册按钮,跳转到Register页面,若注册时两次输入的密码不一致,则弹出对话框提示注册失败,清空用户名框和密码框,光标回位。


②若注册时两次输入密码一致,点击注册按钮,信息通过intent传回Login页面,关闭当前页面并且页面跳转到Login页面,弹出对话框提示注册成功。


③输入注册的信息,点击登录,跳转至Main页面,长时间Toast显示登录信息

附录
1) 在登录页面按下“注册”按钮后,应用程序执行了哪些代码?
//绑定、监听“注册”按钮
button2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent intent = new Intent(Login.this,Register.class);
startActivityForResult(intent,1);//携带信息跳转,请求码1,代表Register页面
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//在一个活动中有可能调用 startActivityForResult()方法去启动很多不同的活动
if (requestCode == 1)
{
//当Login中的Button被触发 onClick() 事件后,把Register中EditText的内容返回给Login。
//返回到Login后,执行ononActivityResult()方法,判断RequestCode和ResultCode无误后,把Login中的EditText的内容改为Register返回的结果。
//通过这样的方式打开这两个Activity,使他们中EditText的内容同步,一个 Activity 改变了,到另一个 Activity 中也会跟着改变。
if (resultCode == RESULT_OK)
{
//注册成功,弹出提示框
new AlertDialog.Builder(Login.this).setTitle("注册成功")
.setMessage("成功注册账号,已自动返回登录页面。")
.setPositiveButton("确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
//finish();
}
}).show();
login_name = data.getStringExtra("regName");
login_pss = data.getStringExtra("regPwd");
}
}
}
}
2)在注册页面按下“注册”按钮后,应用程序执行了哪些代码?
//注册监听
btn1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v)
{
s1 = text1.getText().toString(); //获取注册时输入的新用户名
s2 = text2.getText().toString(); //获取注册时输入的新密码
s3 = text3.getText().toString(); //获取注册时输入的新密码
// 如果两次输入的密码不一致,则提示注册失败
if (!s2.equals(s3))
{
new AlertDialog.Builder(Register.this).setTitle("注册失败")
.setMessage("请确认两次输入的密码一致")
.setPositiveButton("重新注册", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
//finish();
}
}).show();
text1.setText("");//清空用户框
text2.setText("");//清空密码框
text3.setText("");//清空密码框
text1.requestFocus();//获取焦点,光标出现
}
//如果注册成功,则返回登录页面
else
{
Intent intent2=new Intent(); //intent没有参数,仅仅只传递数据
//将用户名和密码通过 putExtra()方法放入intent中,传递给Login页面
intent2.putExtra("regName",text1.getText().toString());
intent2.putExtra("regPwd",text2.getText().toString());
//在子活动中调用setResult方法,设置返回结果 intent2,并且其中包含传递的参数
setResult(RESULT_OK, intent2);
finish();//关闭当前页面
}
}
});
}
}
浙公网安备 33010602011771号