在当前活动上将按钮设置为启用

时间:2015-01-03 13:12:28

标签: java android

我有2个活动,比如说," activity_first.java"和" Activity_second.java"。 在activity_first.java我有2个按钮call activity secondWaitingActivity_second.java也有一个按钮,我称之为Set Waiting to ENABLED。 因此,在运行时,activity_first.java将被呼叫,call activity second已设置 ENABLED,Waiting设置为DISABLED。当您点击call activity second时,Activity_second.java将会显示,但activity_first.java将不会关闭,只需继续运行,这样我就可以回到我手机的后面键盘上了,所以在Activity_second.java如果我点击了Set Waiting to ENABLED按钮,则Waiting中的activity_first.java按钮将设置为已启用,因此如果我关闭Activity_second.java Waiting单击Set Waiting to ENABLED按钮已启用按钮。我的问题是,我不知道如何在Waiting上设置Activity_second.java按钮启用。

any body can help? 
thank you so much!

1 个答案:

答案 0 :(得分:1)

首先,不要写长段,分解你的问题并简单说明:)

您可以通过维护waiting轻松维护static boolean variable按钮的状态 并在onRestart中实施first activity方法。

这是源代码的链接。下载并检查代码。希望你能找到这个有用的

https://drive.google.com/file/d/0B-ZuLKRNNO7BVUZpQVpOYVQ3X0k/view?usp=sharing

随意提出任何问题。

这是代码。

ActivityFirst.java

public class ActivityFirst extends ActionBarActivity implements OnClickListener {

Button btnCallActivitySecond;
Button btnWaiting;

//variable that maintains the state of button
public static boolean status = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);


    btnCallActivitySecond = (Button) findViewById(R.id.btnActCall);
    btnWaiting = (Button) findViewById(R.id.btnWainting);       

    btnWaiting.setEnabled(status);

    btnCallActivitySecond.setOnClickListener(this);
    btnWaiting.setOnClickListener(this);
}
/*This method will be called each time
  whenever you press back button from second activity*/
@Override
protected void onRestart() {
    // TODO Auto-generated method stub
    super.onRestart();
    btnWaiting.setEnabled(status);
    Toast.makeText(this, "restart "+ status, Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch (arg0.getId()) {
    case R.id.btnActCall:
        Intent i = new Intent(this, ActivitySecond.class);
        i.putExtra("status", status);
        startActivity(i);
        break;
    case R.id.btnWainting:
        Toast.makeText(this, " "+ status, Toast.LENGTH_LONG).show();
        break;
    }


}  ... }

ActivitySecond.java

public class ActivitySecond extends ActionBarActivity {

Button btnWaitEnable;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    btnWaitEnable = (Button) findViewById(R.id.btnWaitEnable);

    btnWaitEnable.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            //This statement will return the opposite of what 
            //is stored in that public static boolean variable
            ActivityFirst.status = (ActivityFirst.status == true)?false:true;

        }
    });

}