实验三:Intent、Activity应用

实验三:IntentActivity应用

一、实验目的

本次实验的目的是让大家熟悉Intent和Activity的使用。Intent的最常用的用途是绑定应用程序组件。Intent用来在应用程序Activity间启动、停止和传输。并实现添加用户名,密码小例程。

二、实验要求

  1. 完成Android开发平台的搭建及相关配置
  2. 创建项目并熟悉文件目录结构
  3. 实现例程添加用户名,密码实验步骤

三、实验步骤

项目界面:1添加用户名,密码,在另一页面显示用户名。

 

 

2、无用户名可以先注册

 

 

 

 

1创建项目

新建一个Android工程命名为shiyan3,目录结构如下图:

 

2、添加布局文件

activity_login.xml(登录)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 

    android:id="@+id/login_layout"

 

    android:layout_width="match_parent"

 

    android:layout_height="match_parent"

 

    android:orientation="vertical"

 

    android:padding="16dp">

 

 

 

    <TextView

 

        android:id="@+id/login_title"

 

        android:layout_width="wrap_content"

 

        android:layout_height="wrap_content"

 

        android:text="Login"

 

        android:textSize="24sp"

 

        android:textStyle="bold" />

 

 

 

    <EditText

 

        android:id="@+id/username_edit_text"

 

        android:layout_width="match_parent"

 

        android:layout_height="wrap_content"

 

        android:hint="Username" />

 

 

 

    <EditText

 

        android:id="@+id/password_edit_text"

 

        android:layout_width="match_parent"

 

        android:layout_height="wrap_content"

 

        android:hint="Password"

 

        android:inputType="textPassword" />

 

 

 

    <Button

 

        android:id="@+id/login_button"

 

        android:layout_width="match_parent"

 

        android:layout_height="wrap_content"

 

        android:text="Login" />

 

    <Button

 

        android:id="@+id/next_button"

 

        android:layout_width="match_parent"

 

        android:layout_height="wrap_content"

 

        android:text="registe" />

 

</LinearLayout>

 

activity_display.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 

    android:id="@+id/display_layout"

 

    android:layout_width="match_parent"

 

    android:layout_height="match_parent"

 

    android:orientation="vertical"

 

    android:padding="16dp">

 

 

 

    <TextView

 

        android:id="@+id/display_title"

 

        android:layout_width="wrap_content"

 

        android:layout_height="wrap_content"

 

        android:text="Login Information"

 

        android:textSize="24sp"

 

        android:textStyle="bold" />

 

 

 

    <TextView

 

        android:id="@+id/username_text_view"

 

        android:layout_width="match_parent"

 

        android:layout_height="wrap_content"

 

        android:text="Username: " />

 

 

 

    <TextView

 

        android:id="@+id/password_text_view"

 

        android:layout_width="match_parent"

 

        android:layout_height="wrap_content"

 

        android:text="Password: " />

 

</LinearLayout>

 

 

activity_regester.xml(注册)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/login_layout"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:padding="16dp">

    <TextView

        android:id="@+id/login_title"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="register"

        android:textSize="24sp"

        android:textStyle="bold" />

    <EditText

        android:id="@+id/yonghuming_edit_text"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:hint="用户名" />

    <Button

        android:id="@+id/regester_button"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="下一步输入单位" />

    <TextView

        android:id="@+id/yonghuming_text_view"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="已注册用户名: " />

    <TextView

        android:id="@+id/danwei_text_view"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="已注册单位: " />

 

</LinearLayout>

activity_danwei.xml(单位)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 

    android:id="@+id/login_layout"

 

    android:layout_width="match_parent"

 

    android:layout_height="match_parent"

 

    android:orientation="vertical"

 

    android:padding="16dp">

 

 

 

    <TextView

 

        android:id="@+id/login_title"

 

        android:layout_width="wrap_content"

 

        android:layout_height="wrap_content"

 

        android:text="输入单位"

 

        android:textSize="24sp"

 

        android:textStyle="bold" />

 

 

 

    <EditText

 

        android:id="@+id/danwei_edit_text"

 

        android:layout_width="match_parent"

 

        android:layout_height="wrap_content"

 

        android:hint="单位" />

 

    <Button

 

        android:id="@+id/danwei_button"

 

        android:layout_width="match_parent"

 

        android:layout_height="wrap_content"

 

        android:text="下一步" />

 

</LinearLayout>

 

 

3、创建Activity

 

MainActivity.java:用户登录页面,包括用户名和密码输入框,以及登录按钮和跳转到注册页面的按钮

package com.example.loginandregist;

 

 

 

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 MainActivity extends AppCompatActivity {

 

 

 

    private EditText mUsernameEditText;

 

    private EditText mPasswordEditText;

 

    private Button mLoginButton;

 

 

 

    private Button mNextButton;

 

 

 

    @Override

 

    protected void onCreate(Bundle savedInstanceState) {

 

        super.onCreate(savedInstanceState);

 

        setContentView(R.layout.activity_login);

 

 

 

        mUsernameEditText = findViewById(R.id.username_edit_text);

 

        mPasswordEditText = findViewById(R.id.password_edit_text);

 

        mLoginButton = findViewById(R.id.login_button);

 

        mNextButton = findViewById(R.id.next_button);

 

 

 

        mLoginButton.setOnClickListener(new View.OnClickListener() {

 

            @Override

 

            public void onClick(View v) {

 

                String username = mUsernameEditText.getText().toString();

 

                String password = mPasswordEditText.getText().toString();

 

                Intent intent = new Intent(MainActivity.this, DisplayActivity.class);

 

                intent.putExtra("username", username);

 

                intent.putExtra("password", password);

 

                startActivity(intent);

 

            }

 

        });

 

        mNextButton.setOnClickListener(new View.OnClickListener() {

 

            @Override

 

            public void onClick(View v) {

 

                Intent intent = new Intent(MainActivity.this, RegesterActivity.class);

 

                startActivity(intent);

 

            }

 

        });

 

    }

 

}

 

RegesterActivity.java:用户注册页面,包括用户名、单位、注册按钮和已注册用户名和单位的显示区域

package com.example.loginandregist;

 

 

 

import androidx.appcompat.app.AppCompatActivity;

 

 

 

import android.content.Intent;

 

import android.content.SharedPreferences;

 

import android.os.Bundle;

 

import android.view.View;

 

import android.widget.Button;

 

import android.widget.EditText;

 

import android.widget.TextView;

 

 

 

public class RegesterActivity extends AppCompatActivity {

 

 

 

    private EditText yonghumingEditText;

 

    private TextView yonghumingTextView;

 

    private TextView danweiTextView;

 

 

 

    private Button regesterButton;

 

 

 

    @Override

 

    protected void onCreate(Bundle savedInstanceState) {

 

        super.onCreate(savedInstanceState);

 

        setContentView(R.layout.activity_regester);

 

 

 

        Intent intent = getIntent();

 

        String sharedPreferencesName = intent.getStringExtra("sharedPreferencesName");

 

        SharedPreferences sharedPreferences = getSharedPreferences("my_preferences", MODE_PRIVATE);

 

 

 

        yonghumingEditText = findViewById(R.id.yonghuming_edit_text);

 

        yonghumingTextView = findViewById(R.id.yonghuming_text_view);

 

        // 读取SharedPreferences对象中的数据

 

        String username = sharedPreferences.getString("username", "");

 

        yonghumingTextView.setText("已注册用户名:" + username);

 

        yonghumingEditText.setText(username);

 

 

 

        danweiTextView = findViewById(R.id.danwei_text_view);

 

        String danwei = sharedPreferences.getString("danwei", "");

 

        danweiTextView.setText("已注册单位:" + danwei);

 

 

 

        regesterButton = findViewById(R.id.regester_button);

 

        regesterButton.setOnClickListener(new View.OnClickListener() {

 

            @Override

 

            public void onClick(View v) {

 

                String username = yonghumingEditText.getText().toString();

 

 

 

                // 获取Editor对象

 

                SharedPreferences.Editor editor = sharedPreferences.edit();

 

                // 向SharedPreferences对象中添加数据

 

                editor.putString("username", username);

 

                // 提交数据

 

                editor.commit();

 

 

 

 

 

                Intent intent = new Intent(RegesterActivity.this, DanweiActivity.class);

 

                intent.putExtra("sharedPreferences", "my_preferences");

 

 

 

                startActivity(intent);

 

            }

 

        });

 

 

 

    }

 

}

 

DisplayActivity.java:登录成功后显示用户名和密码的页面

package com.example.loginandregist;

 

 

 

import android.content.Intent;

 

import android.os.Bundle;

 

import android.widget.TextView;

 

 

 

import androidx.appcompat.app.AppCompatActivity;

 

 

 

public class DisplayActivity extends AppCompatActivity {

 

 

 

    private TextView mUsernameTextView;

 

    private TextView mPasswordTextView;

 

 

 

 

 

 

 

    @Override

 

    protected void onCreate(Bundle savedInstanceState) {

 

        super.onCreate(savedInstanceState);

 

        setContentView(R.layout.activity_display);

 

 

 

        mUsernameTextView = findViewById(R.id.username_text_view);

 

        mPasswordTextView = findViewById(R.id.password_text_view);

 

 

 

        Intent intent = getIntent();

 

        String username = intent.getStringExtra("username");

 

        String password = intent.getStringExtra("password");

 

 

 

        mUsernameTextView.setText("用户名:" + username);

 

        mPasswordTextView.setText("密码:" + password);

 

 

 

    }

 

}

 

Danwei.java:用于输入用户所属的单位,并将其存储到SharedPreferences中。

package com.example.loginandregist;

 

 

 

import android.content.Intent;

 

import android.content.SharedPreferences;

 

import android.os.Bundle;

 

import android.view.View;

 

import android.widget.Button;

 

import android.widget.EditText;

 

import android.widget.TextView;

 

 

 

import androidx.appcompat.app.AppCompatActivity;

 

 

 

public class DanweiActivity extends AppCompatActivity {

 

 

 

    private EditText danweiEditText;

 

    private Button danweiButton;

 

 

 

    @Override

 

    protected void onCreate(Bundle savedInstanceState) {

 

        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);

 

        super.onCreate(savedInstanceState);

 

        setContentView(R.layout.activity_danwei);

 

        danweiEditText=findViewById(R.id.danwei_edit_text);

 

        danweiButton = findViewById(R.id.danwei_button);

 

 

 

        danweiButton.setOnClickListener(new View.OnClickListener() {

 

            @Override

 

            public void onClick(View v) {

 

                String danwei = danweiEditText.getText().toString();

 

 

 

                SharedPreferences sharedPreferences = getSharedPreferences("my_preferences", MODE_PRIVATE);

 

                SharedPreferences.Editor editor = sharedPreferences.edit();

 

                editor.putString("danwei", danwei);

 

                editor.apply();

 

 

 

                Intent intent = new Intent(DanweiActivity.this, RegesterActivity.class);

 

                intent.putExtra("sharedPreferencesName", "my_preferences");

 

                startActivity(intent);

 

            }

 

        });

 

    }

 

}

 

 


 

 

posted @ 2025-04-07 09:29  不会JAVA的小袁  阅读(43)  评论(0)    收藏  举报