setText not working for EditTextPreference
I'm trying to set value for EditTextPreference
item
with hostPreference.setText("Not yet set");
But text is not showing
I want to show it like Use Name
here
public class HostSettingActivity extends PreferenceActivity {
private final String MY_DEBUG_TAG = "SettingActivity";
SharedPreferences sharedPrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(MY_DEBUG_TAG, "HostSettingActivity Started");
super.onCreate(savedInstanceState);
sharedPrefs = getPreferenceManager().getSharedPreferences();
se开发者_如何学CtPreferenceScreen(createPreferenceHierarchy());
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(MY_DEBUG_TAG, "On Destroy");
}
private PreferenceScreen createPreferenceHierarchy() {
// Root
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
dialogBasedPrefCat.setTitle("Host Settings");
root.addPreference(dialogBasedPrefCat);
EditTextPreference hostPreference = new EditTextPreference(this);
hostPreference.setKey("host");
hostPreference.setDialogTitle("Host");
hostPreference.setText("Not yet set");
hostPreference.setDefaultValue("http://example.com");
hostPreference.setSummary("Set host");
dialogBasedPrefCat.addPreference(hostPreference);
EditTextPreference portPreference = new EditTextPreference(this);
portPreference.setKey("port");
portPreference.setDialogTitle("Port");
portPreference.setDefaultValue("8080");
portPreference.setSummary("Set port");
dialogBasedPrefCat.addPreference(portPreference);
hostPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
EditTextPreference etp = (EditTextPreference) preference;
String newHostValue = newValue.toString();
Log.i(MY_DEBUG_TAG, "New Host: "+newHostValue);
etp.setText(newHostValue);
return true;
}
});
return root;
}
}
Text is not seen in EditTextPreference, but can be edited.
What is seen is TITLE, and it is set by setTitle.
If you want to display your edited text in title, you must set it as title yourself.
I think you're confusing setText with setTitle...
精彩评论