2014-11-04 23 views
0

我有兩個稱爲主頁和選項的活動。 主頁包含6個按鈕:醫生 - 約會...等 選項包含3個按鈕:添加 - 編輯/刪除 - 列表。一個xml到很多java文件?

選項是一種常見的佈局就意味着我希望它在醫生和約會當按下打開。 但問題是,如果Home中按下的按鈕是Doctors並且「添加約會 - 編輯/刪除約會 - 列出約會」,則分別在options.xml中的按鈕上添加「添加醫生 - 編輯/刪除醫生 - 列表醫生」按鈕「在options.xml中,如果Home中按下的按鈕是預約。

關鍵是要減少活動次數,這樣,而不是與相同的格式創建6個活動,我可以只寫在根據以前的活動按鈕的文字? 這可能嗎?

回答

0

您可以用意圖將信息發送到啓動新的活動。

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; 
} 
+0

它的工作原理。謝謝 ! – user3552658 2014-11-04 18:03:52

+0

@ user3552658不客氣。請不要忘記接受我的回答。 – Robbe 2014-11-04 18:10:15

+0

因爲我試圖讓我的應用程序的多語言,我試圖從串那樣得到消息: intent.putExtra(「BUTTON_TEXT_1」,R.string.apppointment); 但通過這種方式,消息不會出現。這是任何解決方案? – user3552658 2014-11-04 21:35:08