我有2个活动,比如说," activity_first.java
"和" Activity_second.java
"。
在activity_first.java
我有2个按钮call activity second
和Waiting
。
Activity_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!
答案 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;
}
});
}