我们成功地在EditText中设置了错误,但在TextView中设置错误失败了。有什么问题吗?我尝试过了。
((TextView) findViewById(R.id.df)).requestFocus();
((TextView) findViewById(R.id.df)).setSelected(true);
((TextView) findViewById(R.id.df)).setError("akjshbd");
但我没有收到错误的弹出窗口。
我们成功地在EditText中设置了错误,但在TextView中设置错误失败了。有什么问题吗?我尝试过了。
((TextView) findViewById(R.id.df)).requestFocus();
((TextView) findViewById(R.id.df)).setSelected(true);
((TextView) findViewById(R.id.df)).setError("akjshbd");
但我没有收到错误的弹出窗口。
默认的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");
实际上,你可以对 textView 使用 setError 方法并显示其弹出窗口。
你只需要使用与 EditText 相同的样式即可。
只需在 xml 中为 textView 添加以下属性:
style="@android:style/Widget.EditText"
这是唯一需要做的,以获得在TextView上预期的setError行为。
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();
}
});
}