0

EditText 1 つのアクティビティを使用して、別のボタンのテキストを変更しようとしています。私はSharedPreferencesを通過しなければならないことを知っています.これは私が立ち往生している場所ですが.

ボタンを使用したアクティビティ:

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

2 に答える 2

1

これを試して:

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

}

于 2012-04-19T20:33:13.210 に答える
0

共有設定をしばらく無視して、ボタンを含むクラスにpublic static String 変数を持たないのはなぜですか。

public static String buttonText = "somthing";

編集テキストを含むクラスでは、編集テキストの変更をリッスンするイベント ハンドラー、またはボタンが押されたときに発生するイベント ハンドラーを呼び出すことができます。

ButtonActivity.buttonText = text.getText();

次に、ボタンを含むアクティビティの onResume() メソッドで

button.setText(buttonText);

これを試してみてください。これは、あなたが望むことを行うためのより簡単な方法かもしれません. buttonText 変数を宣言するときは、必ずstaticキーワードを使用するようにしてください。それがなければ、静的キーワードを使用してオブジェクトへの直接参照が必要になります。必要なクラスを参照するだけです。ただし、静的なボタン テキストであることは、アクティビティを含むボタンのすべてのインスタンスで同じになります。アクティビティのインスタンスを 1 つだけ持つつもりなら、これが解決策です。そうでない場合は、もう少しクリエイティブになる必要があります。

于 2012-04-19T20:45:45.010 に答える