同じレイアウト、同じボタン配置、同じボタン ID で 2 つのアクティビティを作成しました。すべて同じです
例:
first_activity と second_activity に 3 つのボタンを追加しました。すべてが同じ場所に配置され、id、button1、button2 は同じですが、最後のボタンである「チェック」は可視性を削除に設定しました。
最初の活動
button1 と button2 のバックグラウンド リソースを設定する
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
..................................................
//Change button1 drawable to example1.png
button1 = (Button) ....
button1.setBackgroundResource(R.drawable.example1);
//Change button2 drawable to example1.png
button2 = (Button) ....
button2.setBackgroundResource(R.drawable.example1);
//Check Button
check = (Button) findViewById(R.id.check);
check.setBackgroundResource(R.drawable.example1);
..................................................
}
ボタン1 onClick
public void button1_click (View v){
//If button2 drawable same as check it will changed to another drawable
if (button2.getBackground().getConstantState().equals(check.getBackground().getConstantState())){
button2.setBackgroundResource(R.drawable.example2);
}
else {
button2.setBackgroundResource(R.drawable.example1);
}
}
button2 onClick
public void button2_click (View v){
Intent second_activity=new Intent (getApplicationContext(), second_activity.class);
startActivity(second_activity);
//Calling Second Activity when button2 pressed
finish();
//Finish First Activity
}
条件 :最初に押されたボタン 1、ボタン 2 のドローアブルが example2.png に変更されました。second_activity を起動して first_activity を閉じる 2 番目に押されたボタン 2
私の質問: second_activityの起動後にbutton2ドローアブルをexample2.pngに設定したままにする方法
first_activity と同じように、second_activity で以下のコードを使用しています
第二の活動
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
..................................................
//Change button1 drawable to example1.png
button1 = (Button) ....
button1.setBackgroundResource(R.drawable.example1);
//Change button2 drawable to example1.png
button2 = (Button) ....
button2.setBackgroundResource(R.drawable.example1);
//Check Button
check = (Button) findViewById(R.id.check);
check.setBackgroundResource(R.drawable.example1);
..................................................
}