you can try to addTextChangedListener() which disabes/enables the
positive button (okbutton), add a hint-text to the textfield or a
defaulttext when the dialog is first showing up.
also you can manipulate the textcolor of the edittextfield in the
textchangedlistener to give feedback to the user if he's wrong.
the best would be to implement a TextWatcher i think!?
the textwatcher will give you this methods:
Edittext userInputField = ....;
public void afterTextChanged(Editable s) {
try {
verifyID(s);
okButton.setClickable(true);
okButton.setOnClickListener(this);
userInputField.setTextColor(Color.DKGRAY);
} catch (SomeException e) {
okButton.setClickable(false);
userInputField.setTextColor(Color.RED);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int
count) {
}
where the verifyId() is verifying an emailaddress:
public static void verifyID(String jid)
throws SomeException {
if (id != null) {
Pattern p = Pattern
.compile("[a-z0-9\\-_\\....@[a-z0-9\\-_]++(\\.[a-z0-9\\-_]++)++");
Matcher m = p.matcher(jid);
if (!m.matches()) {
throw new SomeException(
"Configured ID is incorrect!");
}
} else {
throw new SomeException("Jabber-ID wasn't set!");
}
}
public static void verifyID(Editable id)
throws SomeException {
verifyID(id.toString());
}
public static int tryToParseInt(String value, int defVal) {
int ret;
try {
ret = Integer.parseInt(value);
} catch (NumberFormatException ne) {
ret = defVal;
}
Feel free to contact me if you have further questions
Regards,
Christian Polzer
[email protected]
On 05.07.2009, at 12:42, James Lai wrote:
>
> Hi,
>
> I generate a EditText object by "EditText et = new EditText(Context
> c)", then I added this EditText to a AlertDialog. Now I want to
> prevent the user to enter nothing to this EditText (need at least
> enter one char), what should I do? Thanks a lot..
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---