简体   繁体   English

如何在Android上使用EditText更改按钮的文本

[英]How to Change the Text of a button using EditText on Android

I am trying to use an EditText one one Activity to change the text of a button on another. 我试图使用一个EditText一个活动来更改另一个按钮的文本。 I know I have to go through the SharedPreferences, and although this is where I am stuck. 我知道我必须经历SharedPreferences,尽管这是我遇到的问题。

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: 这是我的活动,具有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();
    }
}

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();

} }

Ignoring the shares preferences for a moment, why not just have a public static String variable in the class containing the button. 暂时忽略共享首选项,为什么不只在包含按钮的类中具有公共静态 String变量。

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 然后在包含按钮的活动的onResume()方法中

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. 请记住,在声明buttonText变量时,请确保记住使用static关键字。 Without it you will need a direct reference to the object, with the static keyword, you can just refer to the required class. 没有它,您将需要使用static关键字直接引用该对象,只需引用所需的类即可。 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. 如果不是这样,那么您就必须变得更有创意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM