32

Edittext ではエラーを設定できますが、textview では設定できませんでした。何か問題ある??私は試した

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

しかし、エラーのポップアップが表示されません。

テキストビュー エラー

4

4 に答える 4

98

デフォルトの 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");
于 2013-04-11T16:45:04.977 に答える
42

実際には、textView に setError を使用して、そのポップアップを表示できます。

EditText と同じスタイルを使用するだけです。

xml に textView の次の属性を追加するだけです。

style="@android:style/Widget.EditText"
于 2013-02-17T15:48:23.400 に答える
13

これは、TextView で予期される setError の動作を取得するために必要な唯一のものです。

android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"
于 2014-07-21T12:33:22.393 に答える
5

スニペット: 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();
    }
    });
}
于 2013-01-10T22:43:42.897 に答える