[英]How to set results from next activity to previous activity in android studio
[英]How to store button information from previous activity, Android Studio Edition
所以,我有一個帶有 2 個按鈕 marsPhoto 和 viewByDate 的活動。 單擊按鈕后,他們 go 到一個新的活動頁面,我想從 putExtra(每日或日期)中獲取名稱並打印 TextView 中的每日或日期。 我只想將文本設置為與按鈕的字符串相匹配(對於單擊的意圖 2 是每日,對於單擊的意圖 3 是日期)。
活動一
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.marsPhoto:
Intent intent2 = new Intent(this, MARS.class);
intent2.putExtra("Daily",1);
startActivity(intent2);
break;
case R.id.viewByDate:
Intent intent3 = new Intent(this, MARS.class);
intent3.putExtra("Date",2);
startActivity(intent3);
break;
}
}
活動二
TextView type;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mars);
type = (TextView)findViewById(R.id.type);
Intent i = getIntent();
String s1 = i.getStringExtra("Daily");
String s2 = i.getStringExtra("Date");
type.setText(s1);
}
將傳遞的StringExtra
的鍵更改為類似"button_text"
的內容,並將值設置為要顯示的文本,即"Date"
或"Daily"
。 您要轉移的 integer 似乎沒有任何意義。
將第一個代碼修改為此
活動一
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.marsPhoto:
Intent intent2 = new Intent(this, MARS.class);
intent2.putExtra("dataSent","Daily");
startActivity(intent2);
break;
case R.id.viewByDate:
Intent intent3 = new Intent(this, MARS.class);
intent3.putExtra("dataSent","Date");
startActivity(intent3);
break;
}
}
活動二
TextView type;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mars);
type = (TextView)findViewById(R.id.type);
String s1="";
Bundle extras = getIntent().getExtras();
if(extras == null) {
s1 = "";
} else {
s1 = extras.getString("dataSent","");
}
type.setText(s1);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.