实验一 Android 活动与活动间数据传递

综合运用常用控件完成一个注册与登录的应用程序设计。要求基于常用控件,综合使用Intent实现Android的Activity之间信息交换。系统包含启动页、注册页、登录页3个页面,具体要求如下:

1.注册页面和功能的实现。

- 界面要求包含用户名、密码、重复密码信息。

- 要求在用户输入信息后,单击注册按钮,将用户的注册信息用Toast显示。

2.登陆页面和功能的实现。

- 要求包含用户名、密码,要求包含登陆和注册功能。

- 要求用户输入信息后,对用户名和密码进行检查:默认用户名为“Admin”,密码为“Hello Android”,Toast提示“欢迎进入DIY”,否则提示“用户名或密码不正确,请重试”。

3.上述两个页面进行信息传递。

- 注册的信息能用于登陆页面。第一次打开应用时可以通过注册按钮进行注册;在注册界面完成注册后携带注册数据返回至登陆页面,然后在登录页可使用新注册信息进行登陆,并利用Toast显示登录成功与否的提示信息。(提示:使用启动需要返回值的活动的方法)

4. 为该项目制作一个启动页。

- 项目运行后首先进入启动页,若干秒后进入登录页面。

- 去掉启动页面的标题效果。(自主测试并完成,非必须)

- 参考教材闪屏页案例,改进启动页的效果,即:多张闪屏页完成后进入主页面(登录页)

5.在实验报告最后附录部分回答下面的问题:

1)在登录页面按下“注册”按钮后,应用程序执行了哪些代码?

2)在注册页面按下“注册”按钮后,应用程序执行了哪些代码?

 

package com.example.demo2;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
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;

import java.sql.BatchUpdateException;
import java.time.Instant;

public class LoginActivity extends AppCompatActivity {
    private EditText nameET,passwordET;
    private Button loginBt,registerBt;
    private String userName="Admin";
    private String pw_msg="123456";
    private String input_username,input_pwd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        nameET = (EditText) findViewById(R.id.username);
        passwordET = (EditText) findViewById(R.id.password);
        loginBt = (Button) findViewById(R.id.loginBt);
        registerBt = (Button) findViewById(R.id.registerBt);

        loginBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                input_username = nameET.getText().toString();
                input_pwd = passwordET.getText().toString();

                if (input_username.length() == 0 || input_pwd.length() == 0 ||
                        !input_username.equals(userName) || !input_pwd.equals(pw_msg)) {
                    new AlertDialog.Builder(LoginActivity.this).setTitle("登录信息有误")
                            .setMessage("请输入正确的用户名和密码")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface diaglogInterface,int i){
                            //finfish();
                        }
                    }).show();
                    nameET.setText("");
                    passwordET.setText("");
                    nameET.requestFocus();
                } else {
                    String msg = "欢迎进入DIY!!\n您输入的用户名是:" + input_username + "\n密码是:" +
                            input_pwd;
                    Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_LONG).show();
                }
            }
        });
        registerBt.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent, 1);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                userName = data.getStringExtra("regName");
                pw_msg = data.getStringExtra("regPwd");
            }
        }
    }
}
package com.example.demo2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class RegisterActivity extends AppCompatActivity {
    private EditText nameET,pwdET;
    private Button regBt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        nameET = (EditText) findViewById(R.id.reg_name);
        pwdET = (EditText) findViewById(R.id.reg_pwd);

        regBt=(Button) findViewById(R.id.reg_bt);

        regBt.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent3=new Intent();

                intent3.putExtra("regName",nameET.getText().toString());
                intent3.putExtra("regPwd",pwdET.getText().toString());
                setResult(RESULT_OK,intent3);
                finish();
            }
        });
    }
}
package com.example.demo2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
import android.widget.EditText;

import java.time.Instant;

public class SplashActivity 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(SplashActivity.this,LoginActivity.class);
                startActivity(mainIntent);
                finish();
            }
        },3000);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_margin="10dp"
    tools:context=".LoginActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20sp"
        android:textColor="@android:color/holo_red_dark"
        android:text="欢迎进入DIY" />

    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:inputType="textPersonName"
        android:hint="Name" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:hint="password"
        android:inputType="textPassword" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

    <Button
        android:id="@+id/loginBt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="登录" />

    <Button
        android:id="@+id/registerBt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="注册" />

    </LinearLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_margin="10dp"
    tools:context=".RegisterActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20sp"
        android:textColor="@android:color/holo_red_dark"
        android:text="请输入用户信息"/>

    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="用户名">
        </TextView>

        <EditText
            android:id="@+id/reg_name"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:text=""/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="密 码:"/>
        <EditText
            android:id="@+id/reg_pwd"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:hint="*"/>
    </LinearLayout>

    <Button
        android:id="@+id/reg_bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="注册"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_margin="10dp"
    tools:context=".RegisterActivity">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/jimi1"/>
</LinearLayout>

嗯,前人栽树,后人乘凉

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可我不想做饼干

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值