1、现有两个Activity,分别为UserinfoActivity和ShowActivity,现在要从UserinfoActivity,中通过一个“查看”按钮切换界面到ShowActivity,并传递如下三个参数:id=1:username="eric”:state=True,且ShowActivity使用 Toast 显示接收到的参数,请写出两个Activity中的核心代码。
时间: 2024-03-16 10:41:25 浏览: 219
在UserinfoActivity中,核心代码如下:
```java
public class UserinfoActivity extends AppCompatActivity {
private Button mBtnCheck;
private int mId = 1;
private String mUsername = "eric";
private boolean mState = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_userinfo);
mBtnCheck = findViewById(R.id.btn_check);
mBtnCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(UserinfoActivity.this, ShowActivity.class);
intent.putExtra("id", mId);
intent.putExtra("username", mUsername);
intent.putExtra("state", mState);
startActivity(intent);
}
});
}
}
```
在ShowActivity中,核心代码如下:
```java
public class ShowActivity extends AppCompatActivity {
private TextView mTvShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
mTvShow = findViewById(R.id.tv_show);
Intent intent = getIntent();
int id = intent.getIntExtra("id", 0);
String username = intent.getStringExtra("username");
boolean state = intent.getBooleanExtra("state", false);
String message = String.format("id=%d,username=%s,state=%b", id, username, state);
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
mTvShow.setText(message);
}
}
```
阅读全文