Used AlertDialog within EditTextPreference combined with a TextWatcher to disable setting the preference if the location is too short.
This commit is contained in:
@@ -15,10 +15,17 @@
|
||||
*/
|
||||
package com.example.android.sunshine.app;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.os.Bundle;
|
||||
import android.preference.EditTextPreference;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
|
||||
public class LocationEditTextPreference extends EditTextPreference {
|
||||
static final private int DEFAULT_MINIMUM_LOCATION_LENGTH = 2;
|
||||
@@ -36,5 +43,42 @@ public class LocationEditTextPreference extends EditTextPreference {
|
||||
a.recycle();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void showDialog(Bundle state) {
|
||||
super.showDialog(state);
|
||||
|
||||
EditText et = getEditText();
|
||||
et.addTextChangedListener(new TextWatcher() {
|
||||
|
||||
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
Dialog d = getDialog();
|
||||
if (d instanceof AlertDialog) {
|
||||
AlertDialog dialog = (AlertDialog) d;
|
||||
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
|
||||
// Check if the EditText is empty
|
||||
if (s.length() < mMinLength) {
|
||||
// Disable OK button
|
||||
positiveButton.setEnabled(false);
|
||||
} else {
|
||||
// Re-enable the button.
|
||||
positiveButton.setEnabled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user