1
任何人都可以告訴我這段代碼有什麼問題。出於某種原因,OnSharedPreferencesChanged在更改值時未被調用。我正在嘗試檢查輸入的PIN碼是否等於2位數或否。但它只是不工作.. 任何人都可以幫助我。EditTextPreferences驗證
謝謝。
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.widget.Toast;
public class PrefsActivity extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
EditTextPreference editPreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
this.editPreference = ((EditTextPreference) getPreferenceScreen()
.findPreference("userPass"));
Log.d("TAG","before sharedPreferenceChanged");
}
@Override
protected void onResume() {
super.onResume();
// Set up a listener whenever a key changes
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPause() {
super.onPause();
// Unregister the listener whenever a key changes
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
Log.d("TAG","In SharedPreferences");
if (sharedPreferences.getString("userPass", null).length() != 2) {
Log.d("TAG","lenght is less than 1");
Toast.makeText(this, "Pin has to be 2 digits only",
Toast.LENGTH_LONG).show();
this.editPreference.setText(null);
return;
} else {
Toast.makeText(this, "Pin set", Toast.LENGTH_LONG).show();
}
// TODO Auto-generated method stub
}