Reputation: 1935
I am trying to use an EditText one one Activity to change the text of a button on another. I know I have to go through the SharedPreferences, and although this is where I am stuck.
Activity with the Button:
protected void onResume() {
super.onResume();
class1.setText(this.getButtonText());
}
public String getButtonText()
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String buttonText = prefs.getString("ButtonText", "Default button text"); // I am not sure how to get the button text here. This is what someone was trying to have me do?
return buttonText;
}
This is my Activity that has the EditText and the button to go back to the activity with the button:
public class EditClass1 extends Activity implements OnClickListener{
Button class1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editclass1);
SettingButtons();
class1.setOnClickListener(this);
}
private void SettingButtons() {
// TODO Auto-generated method stub
class1 = (Button) findViewById(R.id.edittoclass1);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.edittoclass1:
startActivity(new Intent("com.clayton.calendar.TOCLASS"));
break;
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = prefs.edit();
editor.putString("ButtonText", // This is not working
((TextView)findViewById(R.id.edittoclass1)).getText().toString());
editor.commit();
}
}
Upvotes: 0
Views: 206
Reputation: 2987
Ignoring the shares preferences for a moment, why not just have a public static String variable in the class containing the button.
public static String buttonText = "somthing";
When in the class containing the edit text you can call in an event handler which listens for changes to the edit text or an event handler that is fired when a button is pressed.
ButtonActivity.buttonText = text.getText();
Then in the onResume() method of the activity containing the button
button.setText(buttonText);
Try this it might be a simpler way of doing what you want. Remember when declaring the buttonText variable make sure you remember to use the static keyword. Without it you will need a direct reference to the object, with the static keyword, you can just refer to the required class. However, being static button Text will be the same for all instances of the button containing activity. If you only ever intend on having one instance of the activity this is the solution for you. If not then you have to get a little more creative.
Upvotes: 0
Reputation: 5535
Try this:
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
EditText text = (EditText)findViewById(R.id.Setclass);
String text2 = text;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = prefs.edit();
editor.putString("ButtonText", // This is not working
((TextView)findViewById(R.id.edittoclass1)).getText().toString());
editor.commit();
}
Upvotes: 1