Android setError ("ошибка") не работает в Textview

мы можем установить ошибку в Edittext успешно, но не удалось установить в textview. какие-то проблемы?? я пытался!--2-->

((TextView) findViewById(R.id.df)).requestFocus();
((TextView) findViewById(R.id.df)).setSelected(true);
((TextView) findViewById(R.id.df)).setError("akjshbd");

но я не получаю всплывающее окно для ошибки.

Textview Error

4 ответов


фактически, вы можете использовать setError для textView и показать его всплывающее окно .

вам просто нужно использовать тот же стиль, что и в EditText .

просто добавьте следующий атрибут для textView в xml:

style="@android:style/Widget.EditText"

по умолчанию TextView не фокусируется. Итак, вам нужно установить android: focusable="true" и android: focusableInTouchMode="true".

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:text="@string/hello_world" />

и нет необходимости устанавливать setSelected (true).

((TextView) findViewById(R.id.df)).requestFocus();
((TextView) findViewById(R.id.df)).setError("akjshbd");

это единственное, что вам нужно, чтобы получить ожидаемое поведение setError в TextView

android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"

фрагмент: вы должны requestFocus (); представление, чтобы показать ошибку.

    // Check for a valid email address.
if (TextUtils.isEmpty(mEmail)) {
    mEmailView.setError(getString(R.string.error_field_required));
    focusView = mEmailView;
    cancel = true;
} else if (!mEmail.contains("@")) {
    mEmailView.setError(getString(R.string.error_invalid_email));
    focusView = mEmailView;
    cancel = true;
}

if (cancel) {
    // There was an error; don't attempt login and focus the first
    // form field with an error.
    focusView.requestFocus();
} else {
    // Show a progress spinner, and kick off a background task to
    // perform the user login attempt.
    // showProgress(true);
    // mAuthTask = new UserLoginTask();
    // mAuthTask.execute((Void) null);
    ParseUser.logInInBackground(mEmail, mPassword, new LogInCallback() {

    @Override
    public void done(ParseUser user, ParseException e) {
        finishAndStartCardActivity();
    }
    });
}