选择三种数据传递方式使用 Intent 传递“用户名、密码和是否登录”信息
时间: 2024-04-30 07:22:50 浏览: 17
1. 使用 Bundle 传递数据:
```java
// 发送方
Intent intent = new Intent(this, LoginActivity.class);
Bundle bundle = new Bundle();
bundle.putString("username", "username");
bundle.putString("password", "password");
bundle.putBoolean("isLogin", true);
intent.putExtras(bundle);
startActivity(intent);
// 接收方
Bundle bundle = getIntent().getExtras();
String username = bundle.getString("username");
String password = bundle.getString("password");
boolean isLogin = bundle.getBoolean("isLogin");
```
2. 使用 Intent 传递数据:
```java
// 发送方
Intent intent = new Intent(this, LoginActivity.class);
intent.putExtra("username", "username");
intent.putExtra("password", "password");
intent.putExtra("isLogin", true);
startActivity(intent);
// 接收方
String username = getIntent().getStringExtra("username");
String password = getIntent().getStringExtra("password");
boolean isLogin = getIntent().getBooleanExtra("isLogin", false);
```
3. 使用 Parcelable 传递数据:
```java
// 发送方
Intent intent = new Intent(this, LoginActivity.class);
User user = new User("username", "password", true);
intent.putExtra("user", user);
startActivity(intent);
// 接收方
User user = getIntent().getParcelableExtra("user");
// User 类的实现
public class User implements Parcelable {
private String username;
private String password;
private boolean isLogin;
public User(String username, String password, boolean isLogin) {
this.username = username;
this.password = password;
this.isLogin = isLogin;
}
protected User(Parcel in) {
username = in.readString();
password = in.readString();
isLogin = in.readByte() != 0;
}
public static final Creator<User> CREATOR = new Creator<User>() {
@Override
public User createFromParcel(Parcel in) {
return new User(in);
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(username);
dest.writeString(password);
dest.writeByte((byte) (isLogin ? 1 : 0));
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)