4.14日报
完成移动应用开发实验三:
实验三:Intent、Activity应用
一、实验目的
本次实验的目的是让大家熟悉Intent和Activity的使用。Intent的最常用的用途是绑定应用程序组件。Intent用来在应用程序Activity间启动、停止和传输。并实现添加用户名,密码小例程。
二、实验要求
- 完成Android开发平台的搭建及相关配置
- 创建项目并熟悉文件目录结构
- 实现例程添加用户名,密码实验步骤
三、实验步骤
2. 创建项目并熟悉文件目录结构
- 创建新项目:
- 打开Android Studio,选择“Start a new Android Studio project”。
- 选择“Empty Activity”模板,点击“Next”。
- 填写项目名称、包名、保存位置等信息,点击“Finish”。
- 熟悉文件目录结构:
- app:包含应用的主要代码和资源文件。
- src:源代码目录,包含Java代码。
- res:资源目录,包含布局文件、图片、字符串等资源。
- AndroidManifest.xml:应用的配置文件,定义了应用的组件和权限。
3. 实现例程添加用户名和密码的实验步骤
我们将创建两个Activity:
- MainActivity:用于输入用户名和密码,并跳转到第二个Activity。
- SecondActivity:用于显示输入的用户名和密码。
3.1 编写MainActivity布局文件
在res/layout/activity_main.xml中编写布局代码:
<?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"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center_horizontal">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"
android:inputType="text"
android:layout_marginBottom="16dp" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"
android:layout_marginBottom="16dp" />
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:layout_marginBottom="16dp" />
</LinearLayout>
3.2 编写MainActivity逻辑代码
在MainActivity.java中编写逻辑代码:
package com.example.intentactivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText etUsername;
private EditText etPassword;
private Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsername = findViewById(R.id.et_username);
etPassword = findViewById(R.id.et_password);
btnLogin = findViewById(R.id.btn_login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
// 创建Intent,用于跳转到SecondActivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("username", username);
intent.putExtra("password", password);
startActivity(intent);
}
});
}
}
3.3 创建SecondActivity
- 创建布局文件: 在res/layout/activity_second.xml中编写布局代码:
<?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"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center_horizontal">
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:layout_marginBottom="16dp" />
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:layout_marginBottom="16dp" />
</LinearLayout>
编写逻辑代码: 在SecondActivity.java中编写逻辑代码
package com.example.intentactivity;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
private TextView tvUsername;
private TextView tvPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tvUsername = findViewById(R.id.tv_username);
tvPassword = findViewById(R.id.tv_password);
// 获取Intent传递的数据
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String username = bundle.getString("username");
String password = bundle.getString("password");
// 显示用户名和密码
tvUsername.setText("用户名:" + username);
tvPassword.setText("密码:" + password);
}
}
}
3.4 配置AndroidManifest.xml
确保在AndroidManifest.xml中注册了两个Activity:
<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.IntentActivity">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" />
</application>