您可以用意圖將信息發送到啓動新的活動。
public void onDoctorClick(View v) {
Intent intent = new Intent(this, nextActivity.class);
intent.putExtra("button_text_1", "Add doctor");
intent.putExtra("button_text_2", "Edit/Delete doctor");
intent.putExtra("button_text_3", "List doctor");
startActivity(intent);
}
public void onAppointmentClick(View v) {
Intent intent = new Intent(this, nextActivity.class);
intent.putExtra("button_text_1", "Add appointment");
intent.putExtra("button_text_2", "Edit/Delete appointment");
intent.putExtra("button_text_3", "List appointment");
startActivity(intent);
}
然後你就可以在你的新的活動得到這一信息並設置按鈕的文字:
Bundle extras = getIntent().getExtras();
button1.setText(extras.getString("button_text_1"));
button2.setText(extras.getString("button_text_2"));
button3.setText(extras.getString("button_text_3"));
編輯
您也可以只發送一個字符串,以指示哪個按鈕啓動活動
intent.putExtra("type", "doctor")
然後在第二個活動中檢查ik:
switch(getIntent().getExtras().getString("type")) {
case "doctor":
button1.setText(R.string.doctor);
break;
case "appointment":
button1.setText(R.string.appointment);
break;
}
它的工作原理。謝謝 ! – user3552658 2014-11-04 18:03:52
@ user3552658不客氣。請不要忘記接受我的回答。 – Robbe 2014-11-04 18:10:15
因爲我試圖讓我的應用程序的多語言,我試圖從串那樣得到消息: intent.putExtra(「BUTTON_TEXT_1」,R.string.apppointment); 但通過這種方式,消息不會出現。這是任何解決方案? – user3552658 2014-11-04 21:35:08