4 回答
TA贡献1805条经验 获得超9个赞
第一个活动中的某个位置 ():MainActivity.java
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
// Pass some data
// Key "DATE_KEY" is static field from SecondActivity
intent.putExtra(DATE_KEY, "20.03.2019");
startActivity(intent);
}
});
在第二个活动中():SecondActivity.java
public class SecondActivity extends AppCompatActivity {
static final String DATE_KEY = "DATE_KEY";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// Read passed data - String "20.03.2019"
String date = getIntent().getStringExtra(DATE_KEY);
}
}
TA贡献1812条经验 获得超5个赞
在调用第二个活动时使用,然后在第二个活动中使用将所选值带回第一个活动:startActivityForResultsetResult
在第一个活动:
void openSecondActivity(){
Intent intent = Intent(context, SecondActivity.class)
startActivityForResult(intent,1004)
}
@Override
void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1004) {
if (resultCode == Activity.RESULT_OK) {
//do something with results
}
super.onActivityResult(requestCode, resultCode, data)
}
在第二个活动状态下:
void returnResult(String result){
Intent returnIntent = new Intent()
returnIntent.putExtra("result", result)
setResult(Activity.RESULT_OK, returnIntent)
finish()
}
TA贡献1809条经验 获得超8个赞
试试这个
在您的主要活动中
buscarButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, json.class).putExtra("extra1",date.getText().toString()).putExtra("extra2",date2.getText().toString()));
}
});
在 json 活动中
if (getIntent().hasExtra("extra1")){
tvJSON.setText(getIntent().getStringExtra("extra1"));
}
if (getIntent().hasExtra("extra2")){
textView.setText(getIntent().getStringExtra("extra2"));
}
TA贡献1830条经验 获得超3个赞
根据文档。
从 FirstActivity 发送数据:
public void sendMessage(View view) {
Intent intent = new Intent(this, SecondActivity.class);
String message = date.getText().toString();
String message2 = date2.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
intent.putExtra(EXTRA_MESSAGE_2, message2);
startActivity(intent);
}
在第二个活动中接收数据:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(FirstActivity.EXTRA_MESSAGE);
String message2 = intent.getStringExtra(FirstActivity.EXTRA_MESSAGE_2);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
添加回答
举报