0

创建了 2 个具有相同布局、相同按钮位置、相同按钮 ID 的活动,一切都相同

例子:

在 first_activity 和 second_activity 中添加了 3 个按钮,所有内容都放置在同一个位置,并且具有相同的 id、button1 和 button2,但最后一个按钮“check”将可见性设置为消失。

第一项活动

为 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);

    ..................................................
}

button1 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
}

条件:首先按下 button1,button2 drawable 更改为 example2.png。Second Pressed button2 将启动 second_activity 并关闭 first_activity

我的问题:如何在启动 second_activity 后将 button2 drawable 设置为 example2.png

我在 second_activity 中使用下面的代码,与 first_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);

    ..................................................
}
4

1 回答 1

1

您可以将“额外”添加到用于启动第二个活动的 Intent。这个“额外”可能包含有关按钮状态的信息。在onCreate()第二个活动中,您可以检查“额外”中的信息并相应地设置按钮的背景。

编辑:添加示例代码

我将为每个按钮创建一个布尔变量,指示背景的状态并将其添加到每个活动中,如下所示:

private boolean button1Checked;
private boolean button2Checked;

然后,每当您更改背景颜色时,都会更改相应变量的状态:

if (...) {
    button2.setBackgroundResource(...);
    button2Checked = true; // or false, whatever
}

然后,当您想启动 activity2 时,将 2 个按钮的状态作为附加信息添加到 Intent 中,如下所示:

Intent intent=new Intent (this, second_activity.class); // use "this" instead of getApplicationContext()
intent.putExtra("button1Checked", button1Checked);
intent.putExtra("button1Checked", button2Checked);
startActivity(intent);

现在,在onCreate()第二个活动中,您从传递的附加项中恢复按钮的状态,如下所示:

button1 = (Button) ....
button1Checked = getIntent().getBooleanExtra("button1Checked", false);
if (button1Checked) {
    button1.setBackgroundResource(...); // true state
} else {
    button1.setBackgroundResource(...); // false state
}

button2 = (Button) ....
button2Checked = getIntent().getBooleanExtra("button2Checked", false);
if (button2Checked) {
    button2.setBackgroundResource(...); // true state
} else {
    button2.setBackgroundResource(...); // false state
}

希望你明白这一点。

于 2013-05-06T16:38:05.037 回答