1

因此,我的首选项中有 EditText,我想获取文本并将其用作字符串。然后我想每次单击按钮时都使用该字符串,它将我的另一个 EditText 文本设置为首选项中的字符串。代码:

<EditTextPreference android:title="Uredi Text"
    android:key="ime"
    android:summary="ime pjesme"
    />

Java代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 
    String b1t = preferences.getString("ime", "DEFAULT");
 et1 = (EditText) findViewById(R.id.editText1);
//Button Click
public void button(View view) {
        et1.setText(b1t);
}
4

3 回答 3

1

编辑:约翰在我之前发布了他的答案,他的答案反映了大多数用例,例如在标准Activity. 我的回答仅针对使用PreferenceActivity.

以下是如何Preference在. 抱歉,如果有任何错误,我正在手机上打字。PreferencePreferenceActivity

您的XML-defined Preferences 将自动将其值保存到 SharedPreferences ,但代码创建Preference的 s 将需要手动保存/持久化。

PreferenceActivity'sonConfigurationChange() 通常保存代码创建的首选项的状态(嵌套的 PreferenceScreens 有点奇怪。)

而且,当然,你会想要做空检查(为了节省我的大拇指而省略了。)

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="Preferences"
    android:key="root">
    <EditTextPreference android:title="Default Pref Text"
        android:key="ime"
        android:summary="ime pjesme"
    />
    <Preference android:title="Click to add a preference."
        android:key="add"
        android:summary=""
    />
</PreferenceScreen>

我的活动.class

public class MyActivity extends PreferenceActivity {
    public void onCreate(Bundle inState) {
        super.onCreate(inState);
        setContentView(R.layout.activity_main);
        getPreferenceScreen.findPreference(PREFERENCE_BUTTON).
          setOnPreferenceClickListener(new OnNewPrefClickListener());
    }
    private class OnNewPrefClickListener implements OnPreferenceClickListener {
        public boolean onPreferenceClick(Preference preference) {
            PreferenceScreen rootScreen = getPreferenceScreen();
            EditTextPreference defaultPref = 
                      (EditTextPreference)rootScreen.findPreference("ime");
            String defaultText = defaultPref.getText();
            EditTextPreference newPref = new EditTextPreference(getApplicationContext());
            newPref.setText(defaultText);
            rootScreen.addPreference(newPref);
            return true;
        }
    }
}
于 2012-11-17T04:15:49.007 回答
1

根据开发人员文档,您可以使用edittextpreference.getText().toString()

于 2012-11-16T20:43:10.797 回答
1

I'm not sure where your error is so I'll just give you my code for doing this.

    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext();

         String string = sharedPreferences
                .getString("key", "default");

And in my preferences

    <EditTextPreference
        android:defaultValue="default"
        android:key="key"
        android:summary="I'm a summary"
        android:title="I'm a title" />

For the onCLick method to modify the edit text

  EditText editText = (EditText) 
        findViewById(R.id.editText);

  editText.setText(String.valueOf(string));

This code works. If your application still crashes then your issue is either with your onClick event or something else. I personally prefer onClickListeners for buttons.

Heres the code for an onClickListener.

 Button button = (Button)
      findViewById(R.id.button);

 button.setOnClickListener(new OnClickListener() 
 {
     @Override
     public void onClick(View v) 
     {

          //Do some awesome stuff

     }
 });
于 2012-11-17T03:06:57.637 回答