安卓实验3 Intent、Activity的使用

实验题目:IntentActivity的使用

  • 实验目的

1)熟悉Intent和Activity的使用。

2)掌握Activity之间传递参数的方式。

  • 实验内容

1)创建一个登录界面;在第一个界面输入用户名、密码,第二个界面显示登录的信息。

2)创建一个会员注册程序;第一个界面输入用户名,下一步之后,进入第二个界面输入用户的单位,从第二个界面返回后的得到用户的信息。

  • 程序设计思想(流程图,或算法思想或设计方案等)

这个程序是一个简单的登录和注册应用程序,主要包括以下几个页面:

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

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

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

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

在这个应用程序中,主要用到了SharedPreferences,用于存储用户注册信息和单位信息,用Intent来在Activity间传递SharedPreferences。程序设计思路如下:

用户在RegesterActivity中输入用户名和单位,点击注册按钮后将这些信息存储到SharedPreferences中。

用户在MainActivity中输入用户名和密码,点击登录按钮后,程序将检查用户名和密码是否正确,如果正确则跳转到DisplayActivity中显示用户名和密码。

如果用户还没有注册,可以点击MainActivity中的跳转按钮跳转到RegesterActivity中进行注册。

在RegesterActivity中,用户输入的单位将被存储到SharedPreferences中,供DanweiActivity中使用。

在DanweiActivity中,用户可以输入所属单位,并将其存储到SharedPreferences中。

在DisplayActivity中,程序从Intent中获取用户名和密码,并将其显示在界面上。

  • 源代码(主要源代码)

Danwei.java

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);

            }

        });

    }

}

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);



    }

}

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);

            }

        });



    }

}

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>

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_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_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>
  • 测试与运行
  1. 在调试程序的过程中遇到什么问题,是如何解决的?

我在尝试将一个SharedPreferences对象通过Intent传递给另一个Activity,并在另一个Activity中使用它。但是我在尝试将SharedPreferences对象作为Extra放入Intent中时遇到了问题,因为Intent只能传递一些特定的数据类型,而SharedPreferences不是其中之一。

可以使用SharedPreferences的名称作为Extra放入Intent中,然后在接收Intent的另一个Activity中使用SharedPreferences的名称来获取SharedPreferences对象。这样我就可以在两个Activity之间共享SharedPreferences对象了。

  1. 测试数据及运行结果。(无测试数据的,直接记录运行结果)

登录功能:

1.登录界面: 2登录后:

注册功能:

1输入用户名:2输入单位

3下一步返回:

  • 总结

本次登录注册实验涉及到了Android Studio的使用、SharedPreferences的基本操作、以及Intent的使用。在实验过程中,需要注意以下几点:

在使用SharedPreferences时,需要注意指定SharedPreferences的名称和访问模式,如MODE_PRIVATE等。

在使用Intent传递数据时,需要注意传递的数据类型,如字符串、整数等。

在实现登录功能时,需要注意对输入的用户名和密码进行校验。

在实现注册功能时,需要注意将注册的用户名和单位信息保存到SharedPreferences中。

在实现页面跳转时,需要注意Intent的使用,包括指定要跳转的Activity和传递的数据。

在调试过程中,需要注意查看日志和调试信息,以便发现和解决问题。同时,可以利用Android Studio提供的调试工具和模拟器进行调试和测试,以提高程序的稳定性和可靠性。

总之,本次实验涵盖了Android开发的基本知识点,对于初学者来说是一次非常有价值的练习,希望能够对你有所帮助。

  • 19
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值