Android setError(“错误”)无法在Textview中运行

时间:2012-11-22 08:08:52

标签: android error-handling popup textview

我们可以在Edittext中成功设置错误,但无法在textview中设置。有什么问题吗?? 我试过了

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

但我没有弹出错误。

Textview Error

4 个答案:

答案 0 :(得分:89)

默认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");

答案 1 :(得分:34)

实际上,您可以对textView使用setError并显示其弹出窗口。

您只需要使用与EditText相同的样式。

只需在xml中添加textView的下一个属性:

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

答案 2 :(得分:11)

这是唯一需要在TextView上获得预期的setError行为

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

答案 3 :(得分:5)

Snippet:你必须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();
    }
    });
}
相关问题