我想使用EditText one one Activity來更改另一個按鈕上的文本。我知道我必須通過SharedPreferences,儘管這是我卡住的地方。如何使用Android上的EditText更改按鈕的文本
活動與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;
}
這是我的活動具有的EditText和按鈕返回到與按鈕的活動:
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();
}
}
肯定這'String text2 = text;'會工作嗎? – waqaslam 2012-04-19 20:46:26
它應該是'字符串text2 = text.getText()。toString()' – zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 2012-04-19 20:48:24