下面列出了android.widget.EditText#setError ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static boolean validated(Context context, EditText link) {
if (link == null) {
return false;
}
String linkText = link.getText().toString().trim();
if ("".equals(linkText)) {
link.setError(context.getString(R.string.video_collection_template_link_hint));
return false;
} else if (!(linkText.contains(YOUTUBE + ".com") || linkText.contains(YOUTUBE_SHORT) || linkText.contains(DAILYMOTION + ".com") || linkText.contains(VIMEO + ".com"))) {
link.setError(context.getString(R.string.video_collection_template_linited_links));
return false;
}
return true;
}
protected static boolean validateThemeID(Context context, EditText editName, boolean grabFocus )
{
boolean isValid = true;
editName.setError(null);
String themeID = editName.getText().toString().trim();
if (themeID.isEmpty())
{
isValid = false; // themeName is required
editName.setError(context.getString(R.string.edittheme_error_themeName_empty));
if (grabFocus)
editName.requestFocus();
}
if (mode == UIMode.ADD_THEME && WidgetThemes.valueOf(editName.getText().toString()) != null)
{
isValid = false; // themeName is already taken
editName.setError(context.getString(R.string.edittheme_error_themeName_unique));
if (grabFocus)
editName.requestFocus();
}
return isValid;
}
private boolean validateData(EditText question, EditText answer, Context context) {
String questionText = question.getText().toString().trim();
String answerText = answer.getText().toString().trim();
if ("".equals(questionText)) {
question.setError(context.getString(R.string.enter_question));
return false;
} else if ("".equals(answerText)) {
answer.setError(context.getString(R.string.enter_answer));
return false;
} else if (!mIsPhotoAttached) {
Toast.makeText(context, context.getString(R.string.flash_template_attach_image), Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
private float validateAndGetInput(View view) {
EditText editText = view.findViewById(R.id.float_input);
String text = editText.getText().toString();
float newValue = -1;
if (text.isEmpty()) {
editText.setError(getResources().getString(R.string.error_value_required));
return newValue;
}
try {
newValue = Float.valueOf(text.replace(',', '.'));
}
catch (NumberFormatException ex) {
newValue = -1;
}
if (newValue < 0 || newValue > getMaxValue()) {
editText.setError(getResources().getString(R.string.error_value_range));
newValue = -1;
}
return newValue;
}
public static boolean isValid(EditText editText, String regex, String errMsg, boolean required) {
String text = editText.getText().toString().trim();
// clearing the error, if it was previously set by some other values
editText.setError(null);
// text required and editText is blank, so return false
if (required && !hasText(editText)) return false;
// pattern doesn't match so returning false
if (required && !Pattern.matches(regex, text)) {
editText.setError(errMsg);
return false;
}
;
return true;
}
private boolean validateForm(EditText emailField, EditText passwordField) {
boolean result = true;
if (TextUtils.isEmpty(emailField.getText().toString())) {
emailField.setError("Required");
result = false;
} else {
emailField.setError(null);
}
if (TextUtils.isEmpty(passwordField.getText().toString())) {
passwordField.setError("Required");
result = false;
} else {
passwordField.setError(null);
}
return result;
}
@Test
@UiThreadTest
public void hasErrorTextReturnsTrue_WithCorrectErrorString() {
EditText editText = new EditText(context);
editText.setError("TEST");
assertTrue(hasErrorText("TEST").matches(editText));
}
private void savePassword() {
edtPassword = (EditText) findViewById(R.id.newPassword);
edtPassword.setError(null);
if (TextUtils.isEmpty(edtPassword.getText())) {
edtPassword.setError("Password required");
edtPassword.requestFocus();
}
user.setPassword(edtPassword.getText().toString());
loginProcess = new LoginProcess();
loginProcess.execute(user.getDatabase(), user.getHost());
}
@Test
@UiThreadTest
public void hasErrorTextReturnsFalse_WithDifferentErrorString() {
EditText editText = new EditText(context);
editText.setError("TEST");
assertFalse(hasErrorText("TEST1").matches(editText));
}
private boolean checkEditText(EditText code, String verify) {
String check = code.getText().toString();
if(check.length() != 4) return false;
boolean correct = true;
if (verify != null) {
if (!check.equals(verify)) {
correct = false;
}
}
try {
Integer.parseInt(check);
} catch (NumberFormatException nfe) {
correct = false;
}
if (!correct) {
code.setError(mContext.getResources().getString(
R.string.rescue_code_incorrect));
return false;
}
code.setError(null);
View nextFocusDown = mLayoutRoot.findViewById(code.getNextFocusDownId());
if (nextFocusDown != null) nextFocusDown.requestFocus();
return true;
}
private static boolean validated( Context context, EditText title, EditText passage, EditText timer) {
if (title == null || passage == null || timer == null) {
return false;
}
String titleText = title.getText().toString().trim();
String passageText = passage.getText().toString().trim();
String timerText = timer.getText().toString().trim();
if ("".equals(titleText)) {
title.setError(context.getString(R.string.comprehension_template_title_hint));
return false;
} else if ("".equals(passageText)) {
passage.setError(context.getString(R.string.comprehension_template_passage_hint));
return false;
}else if (timerText.length() > 9) {
timer.setError(context.getString(R.string.comprehension_template_timer_correct_hint));
return false;
}else if ("0".equals(timerText)) {
timer.setError((context.getString(R.string.time_zero_error)));
return false;
} else if ("".equals(timerText)) {
timer.setError(context.getString(R.string.comprehension_template_timer_hint));
return false;
}
return true;
}
@Override
public void showEmptyTaskError() {
Snackbar snackbar = Snackbar.make(contentView, getString(R.string.empty_task_message), Snackbar.LENGTH_SHORT);
snackbar.show();
EditText title = getActivity().findViewById(R.id.add_task_title);
title.setError(getResources().getString(R.string.add_task_empty_title));
title.setHintTextColor(Color.RED);
}
public static void removeError(EditText editText) {
TextInputLayout layout = getTextInputLayout(editText);
if (layout != null) {
layout.setErrorEnabled(false);
layout.setError(null);
} else {
editText.setError(null);
}
}
private boolean isEmpty(EditText et) {
if (TextUtils.isEmpty(et.getText())) {
et.setError("不能为空");
return true;
}
return false;
}
/**
* 检测EditText内容是否为空,若为空则进行警告提醒
*
* @param et
* @param msg
* @return
*/
public static boolean showEditTextWarning(EditText et, String msg) {
if (TextUtils.isEmpty(et.getText())) {
et.setError(msg);
et.requestFocus();
et.setText("");
return true;
}
return false;
}
/**
* Uploads the new photo to the backend.
*/
private void uploadNewPhoto() {
EditText editText = (EditText) getActivity().findViewById(R.id
.editTextUploadDescription);
String description = editText.getText().toString();
String error = "";
if(!description.equals("") && mBitmap != null) {
new UploadTask().execute(description);
} else {
if(description.equals("")) {
editText.setError(getString(R.string
.upload_photo_description_missing));
error += getString(R.string.upload_photo_description_missing);
}
if (mBitmap == null) {
if(!error.equals("")) {
error += "\n";
}
error += getString(R.string.upload_photo_photo_missing);
}
new AlertDialog.Builder(getActivity())
.setTitle(getString(R.string
.upload_photo_error_dialog_title))
.setMessage(error)
.setNeutralButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(R.drawable.ic_error_black_48dp)
.show();
}
}
private void savePassword() {
edtPassword = (EditText) findViewById(R.id.newPassword);
edtPassword.setError(null);
if (TextUtils.isEmpty(edtPassword.getText())) {
edtPassword.setError("Password required");
edtPassword.requestFocus();
}
user.setPassword(edtPassword.getText().toString());
loginProcess = new LoginProcess();
loginProcess.execute(user.getDatabase(), user.getHost());
}
public static boolean hasText(EditText editText) {
String text = editText.getText().toString().trim();
editText.setError(null);
// length 0 means there is no text
if (text.length() == 0) {
editText.setError(REQUIRED_MSG);
return false;
}
return true;
}
private void loginUser() {
Log.v("", "LoginUser()");
String serverURL = createServerURL((mSelfHostedURL) ? edtSelfHosted.getText().toString() :
OConstants.URL_ODOO);
String databaseName;
edtUsername = (EditText) findViewById(R.id.edtUserName);
edtPassword = (EditText) findViewById(R.id.edtPassword);
if (mSelfHostedURL) {
edtSelfHosted.setError(null);
if (TextUtils.isEmpty(edtSelfHosted.getText())) {
edtSelfHosted.setError(OResource.string(this, R.string.error_provide_server_url));
edtSelfHosted.requestFocus();
return;
}
if (databaseSpinner != null && databases.size() > 1 && databaseSpinner.getSelectedItemPosition() == 0) {
Toast.makeText(this, OResource.string(this, R.string.label_select_database), Toast.LENGTH_LONG).show();
findViewById(R.id.controls).setVisibility(View.VISIBLE);
findViewById(R.id.login_progress).setVisibility(View.GONE);
return;
}
}
edtUsername.setError(null);
edtPassword.setError(null);
if (TextUtils.isEmpty(edtUsername.getText())) {
edtUsername.setError(OResource.string(this, R.string.error_provide_username));
edtUsername.requestFocus();
return;
}
if (TextUtils.isEmpty(edtPassword.getText())) {
edtPassword.setError(OResource.string(this, R.string.error_provide_password));
edtPassword.requestFocus();
return;
}
findViewById(R.id.controls).setVisibility(View.GONE);
findViewById(R.id.login_progress).setVisibility(View.VISIBLE);
mLoginProcessStatus.setText(OResource.string(OdooLogin.this,
R.string.status_connecting_to_server));
if (mConnectedToServer) {
databaseName = databases.get(0);
if (databaseSpinner != null) {
databaseName = databases.get(databaseSpinner.getSelectedItemPosition());
}
mAutoLogin = false;
loginProcess(databaseName);
} else {
mAutoLogin = true;
try {
Odoo.createInstance(OdooLogin.this, serverURL).setOnConnect(OdooLogin.this);
} catch (OdooVersionException e) {
e.printStackTrace();
}
}
}
@Override
public void handleError(@NonNull final FieldValidationException e) {
final EditText textView = e.getTextView();
final String message = e.getMessage();
textView.setError(message);
}