下面列出了android.widget.TextView#setError ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Auxiliar function to get questions 8 to 10 value and set an error if they have not been
* answered.
*
* @param i Number of question
* @param id {@link RatingStars} id
* @param text {@link TextView} id of the question title
* @param error {@link TextView} title of the first question title whose question has an error
* @return <code>true</code> if an error was set; <code>false</code> otherwise.
*/
private TextView NumberPickerAnswered(int i, int id, int text, TextView error) {
NumberPicker np = (NumberPicker) findViewById(id);
int value = np.getValue();
TextView tv = (TextView) findViewById(text);
if (value == 0) {
tv.setError("");
if (error == null) {
return tv;
}
} else {
questions[i] = ((i == 7) ? (value - 1) * 10 : (value - 1));
tv.setError(null);
}
return error;
}
public void setTotal(TextView totalText, Total total) {
if (total.isError()) {
setTotalError(totalText);
} else {
setAmountText(totalText, total);
totalText.setError(null);
}
}
private void addAmountAndErrorNode(Total total) {
TextView data = x.addInfoNode(layout, -1, R.string.not_available, "");
Drawable dr = getResources().getDrawable(R.drawable.total_error);
dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
if (total.currency == Currency.EMPTY) {
data.setText(R.string.currency_make_default_warning);
} else {
data.setText(total.getError(AbstractTotalsDetailsActivity.this));
}
data.setError("Error!", dr);
}
public static void setError(TextView textView, String errorMessage) {
TextInputLayout textInputLayout = getTextInputLayout(textView);
if (textInputLayout != null) {
textInputLayout.setErrorEnabled(!TextUtils.isEmpty(errorMessage));
textInputLayout.setError(errorMessage);
} else {
textView.setError(errorMessage);
}
}
private void validate(Dialog dialog, TextView addressView, TextView textView) {
// retrieve link address and do some cleanup
final String address = addressView.getText().toString().trim();
boolean isEmail = sEmailValidator.isValid(address);
boolean isUrl = sUrlValidator.isValid(address);
if (requiredFieldValid(addressView) && (isUrl || isEmail)) {
// valid url or email address
// encode address
String newAddress = Helper.encodeQuery(address);
// add mailto: for email addresses
if (isEmail && !startsWithMailto(newAddress)) {
newAddress = "mailto:" + newAddress;
}
// use the original address text as link text if the user didn't enter anything
String linkText = textView.getText().toString();
if (linkText.length() == 0) {
linkText = address;
}
EventBus.getDefault().post(new LinkEvent(LinkFragment.this, new Link(linkText, newAddress), false));
try { dialog.dismiss(); } catch (Exception ignore) {}
} else {
// invalid address (neither a url nor an email address
String errorMessage = getString(R.string.rte_invalid_link, address);
addressView.setError(errorMessage);
}
}
/**
* Auxiliar function to get questions 1 to 6 value and set an error if they have not been
* answered.
*
* @param i Number of question
* @param rating {@link RatingStars} id
* @param text {@link TextView} id of the question title
* @param error {@link TextView} title of the first question title whose question has an error
* @return <code>true</code> if an error was set; <code>false</code> otherwise.
*/
private TextView RatingStarsAnswered(int i, int rating, int text, TextView error) {
RatingStars r = (RatingStars) findViewById(rating);
questions[i] = r.getAnswer();
TextView tv = (TextView) findViewById(text);
if (questions[i] == 10) { // default value is 10
tv.setError("");
if (error == null) return tv;
} else {
tv.setError(null);
}
return error;
}
public static void addError(Context ctx, Object f, TextView label, int strid) {
if (f instanceof EditText) {
((EditText) f).setError(strid == 0 ? null : ctx.getString(strid));
} else if (f instanceof List && ((List) f).get(0) instanceof EditText) {
((List<EditText>) f).get(1).setError(strid == 0 ? null : ctx.getString(strid));
} else if (label != null) {
label.setError(strid == 0 ? null : ctx.getString(strid));
}
}
static boolean setErrorMessageOnTextView(View inputView, String message){
if (TextView.class.isAssignableFrom(inputView.getClass())) {
final TextView text = (TextView) inputView;
text.setError(message);
return true;
} else {
return false;
}
}
private void validate(DialogInterface dialog, TextView addressView, TextView textView) {
// retrieve link address and do some cleanup
final String address = addressView.getText().toString().trim();
boolean isEmail = sEmailValidator.isValid(address);
boolean isUrl = sUrlValidator.isValid(address);
if (requiredFieldValid(addressView) && (isUrl || isEmail)) {
// valid url or email address
// encode address
String newAddress = Helper.encodeUrl(address);
// add mailto: for email addresses
if (isEmail && !startsWithMailto(newAddress)) {
newAddress = "mailto:" + newAddress;
}
// use the original address text as link text if the user didn't enter anything
String linkText = textView.getText().toString();
if (linkText.length() == 0) {
linkText = address;
}
EventBus.getDefault().post(new LinkEvent(LinkFragment.this, new Link(linkText, newAddress), false));
try { dialog.dismiss(); } catch (Exception ignore) {}
} else {
// invalid address (neither a url nor an email address
String errorMessage = getString(R.string.rte_invalid_link, address);
addressView.setError(errorMessage);
}
}
protected boolean isValidUrl(TextView view) {
if (view != null && !URLUtil.isValidUrl(editTextIpcamUrl.getText().toString())) {
view.setError(validationUrl);
view.requestFocus();
return false;
}
return true;
}
protected boolean isValidRequired(TextView view) {
if (utilityService.isBlankText(view)
|| view.getText().equals(labelNone)
|| view.getText().equals(labelMissingGateway)) {
view.setError(validationRequired);
view.requestFocus();
return false;
}
return true;
}
private void setTotalError(TextView totalText) {
totalText.setText(R.string.not_available);
Drawable dr = context.getResources().getDrawable(R.drawable.total_error);
dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
totalText.setError(totalText.getText(), dr);
}
public static void setError(TextView view, CharSequence error) {
view.setError(Texts.withColor(error, Color.RED));
}
private void showError(int componentId, int stringId) {
TextView field = (TextView) findViewById(componentId);
field.setError(getString(stringId));
}
private void showError(int componentId, int stringId) {
TextView field = (TextView) findViewById(componentId);
field.setError(getString(stringId));
}
public static void setError(TextView view, CharSequence error) {
view.setError(Texts.withColor(error, Color.RED));
}
private boolean validForm() {
boolean check = true;
TextView[] fields = new TextView[] {mHost, mUsername, mPassword};
for (TextView field : fields) {
if(field.getText().toString().trim().isEmpty()) {
field.setError(getString(R.string.cannot_be_blank));
check = false;
}
}
return check;
}
private void showError(int componentId, int stringId) {
TextView field = (TextView) findViewById(componentId);
field.setError(getString(stringId));
}
/**
* Eliminate the error on terms and conditions {@link TextView} if the terms and conditions
* {@link CheckBox} is clicked.
*
* @param view
* @see TextView#setError(CharSequence)
*/
public void onCheckboxClicked(View view) {
TextView terms_error = (TextView) findViewById(R.id.terms_text);
terms_error.setError(null);
}
/**
* Eliminate the error on terms and conditions {@link TextView} if the terms and conditions
* {@link CheckBox} is clicked.
*
* @param view
* @see TextView#setError(CharSequence)
*/
public void onCheckboxClicked(View view) {
TextView terms_error = (TextView) findViewById(R.id.terms_text);
terms_error.setError(null);
}