2014-11-04 3 views
0

본인에게는 홈 및 옵션이라는 두 가지 활동이 있습니다. 홈에는 6 개의 버튼이 있습니다. 의사 - 약속 ... 옵션에는 추가 - 수정/삭제 - 목록의 3 가지 버튼이 있습니다.많은 Java 파일에 하나의 XML?

옵션은 의사와 약속을 누를 때 열리려는 일반적인 레이아웃입니다. 그러나 문제는 집에있는 버튼이 의사이고 "약속 추가 - 약속 편집/삭제 - 약속 나열"이면 options.xml에 "의사 추가 - 의사 삭제 - 의사 목록 쓰기"버튼에 각각 쓰기를 원하는 것입니다. 홈에서 누른 버튼이 약속 일 경우 options.xml에 있습니다.

요점은 액티비티의 수를 줄여서 동일한 액티비티로 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

+0

@ user3552658 여러분을 환영합니다. 제 대답을 수락하는 것을 잊지 마십시오. – Robbe

+0

내 응용 프로그램을 여러 언어로 만들려고하기 때문에 나는 다음과 같은 문자열에서 메시지를 가져 오려고했습니다. intent.putExtra ("button_text_1", R.string.apppointment); 그러나 이렇게하면 메시지가 나타나지 않습니다. 그게 해결책이야? – user3552658

관련 문제