实验三:Intent、Activity应用 一、实验目的
实验三:Intent、Activity应用
一、实验目的
本次实验的目的是让大家熟悉Intent和Activity的使用。Intent的最常用的用途是绑定应用程序组件。Intent用来在应用程序Activity间启动、停止和传输。并实现添加用户名,密码小例程。
二、实验要求
- 完成Android开发平台的搭建及相关配置
- 创建项目并熟悉文件目录结构
- 实现例程添加用户名,密码实验步骤
三、实验步骤
一、基础实现步骤
1. 创建两个Activity
java
// LoginActivity.java(主Activity)
public class LoginActivity extends AppCompatActivity {
// 代码将在后续步骤添加
}
// WelcomeActivity.java
public class WelcomeActivity extends AppCompatActivity {
// 代码将在后续步骤添加
}
2. 登录界面布局(activity_login.xml)
xml
<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="24dp">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="密码"/>
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"/>
</LinearLayout>
3. 实现登录跳转逻辑
java
// LoginActivity.java
public class LoginActivity extends AppCompatActivity {
private EditText etUsername, etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etUsername = findViewById(R.id.et_username);
etPassword = findViewById(R.id.et_password);
Button btnLogin = findViewById(R.id.btn_login);
btnLogin.setOnClickListener(v -> {
// 获取输入数据
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
// 创建显式Intent
Intent intent = new Intent(LoginActivity.this, WelcomeActivity.class);
// 附加数据
intent.putExtra("EXTRA_USERNAME", username);
intent.putExtra("EXTRA_PASSWORD", password);
// 启动Activity
startActivity(intent);
});
}
}
4. 欢迎界面实现
xml
<!-- activity_welcome.xml -->
<TextView
android:id="@+id/tv_welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
java
// WelcomeActivity.java
public class WelcomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
TextView tvWelcome = findViewById(R.id.tv_welcome);
// 获取传递的数据
Intent intent = getIntent();
String username = intent.getStringExtra("EXTRA_USERNAME");
String password = intent.getStringExtra("EXTRA_PASSWORD");
// 显示欢迎信息
String welcomeText = "欢迎用户:" + username + "\n密码长度:" + password.length();
tvWelcome.setText(welcomeText);
}
}
5. 修改AndroidManifest.xml
xml
<activity
android:name=".LoginActivity"
android:label="登录页面">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".WelcomeActivity"
android:label="欢迎页面"/>
二、扩展实现建议
1. 数据验证 :
java
// 在点击事件中添加验证
if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
2. 返回数据 :
java
// 使用startActivityForResult
startActivityForResult(intent, REQUEST_CODE);
// 在WelcomeActivity返回数据
Intent returnIntent = new Intent();
returnIntent.putExtra("RESULT_DATA", "操作完成");
setResult(RESULT_OK, returnIntent);
finish();
3. 隐式Intent :
java
// 调用系统功能
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, "example@test.com");
startActivity(Intent.createChooser(emailIntent, "发送邮件"));
四、碰见的问题解决方案
1. Activity未注册 :
xml
<!-- 检查AndroidManifest.xml是否声明了所有Activity -->
2. 数据传递失败 :
java
// 检查Extra的key是否完全一致
intent.putExtra("EXTRA_USERNAME", username); // 发送端
getIntent().getStringExtra("EXTRA_USERNAME"); // 接收端