现有两个Activity,分别为UserlnfoActivity和ShowActivity中通过一个"查看"按钮切换界面到ShowActivity,并传递如下三个参数:id=1;username="eric":state=Ture,且ShowActivity使用Toast显示接收到的参数,请写出两个Activity中的核心代码
时间: 2024-03-16 21:41:32 浏览: 77
UserInfoActivity中的代码:
```java
public class UserInfoActivity extends AppCompatActivity {
private Button mBtnView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_info);
mBtnView = findViewById(R.id.btn_view);
mBtnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(UserInfoActivity.this, ShowActivity.class);
intent.putExtra("id", 1);
intent.putExtra("username", "eric");
intent.putExtra("state", true);
startActivity(intent);
}
});
}
}
```
ShowActivity中的代码:
```java
public class ShowActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
Intent intent = getIntent();
int id = intent.getIntExtra("id", 0);
String username = intent.getStringExtra("username");
boolean state = intent.getBooleanExtra("state", false);
String message = "id: " + id + "\nusername: " + username + "\nstate: " + state;
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
```
阅读全文