我想要的是:
button1
的可见性将设置为true(setVisibility(true)
)。第一页
public class FirstpageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstpage);
final Button bt1 = (Button) findViewById(R.id.chp1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(FirstpageActivity.this, Secondform.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.firstpage, menu);
return true;
}
}
第二页
public class Secondform extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondform);
final Button bt1 = (Button) findViewById(R.id.button1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Secondform.this, Thirdform.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.secondform, menu);
return true;
}
}
答案 0 :(得分:1)
我假设FirstPageActivity
中Button的初始状态设置为Invisible。
因此,为了存储全局定义,您可以使用SharedPrefrences
。
在FirstpageActivity
内onCreate()
方法中,请使用:
SharedPreferences prefs = this.getSharedPreferences("MyPref", 0); // "0" is for private mode
Boolean btn = pref.getBoolean("btnState", null); //Checking if value exists
if (btn==null){
//No value exists
} else if (btn) { //checking if btn is true
bt1.setVisibility(View.VISIBLE);
} else { //if false
bt1.setVisibility(View.INVISIBLE);
}
现在,在您的Secondform
活动中,在您希望更改FirstpageActivity
按钮可见性的Button方法中,添加如下:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putBoolean("btnState", true); //to make the button visible
//editor.putBoolean("btnState", false); //to make the button invisible
editor.commit();
就是这样,你完成了。 一旦了解了它的工作原理,就可以按照自己的方式实现它。
希望这有帮助!