我在表单中有五个字段,并且在验证错误消息和浮动标签上工作正常,但在我在特定requestFocus()
对象上调用TextInputLayout
方法时出现错误,则不是聚焦。
代码段:
BetterSpinner Spinner_material = (BetterSpinner)rfqItem.findViewById(R.id.Spinner_material);
if(Spinner_material.getText()!=null && Spinner_material.getText().toString().length()>0)
{
TextInputLayout_category.setError(null);
TextInputLayout_category.setErrorEnabled(false);
}
else
{
if(isReporting)
{
TextInputLayout_category.setErrorEnabled(true);
TextInputLayout_category.setError(baseActivity.getString(R.string.addrfq_validationerror_material_category));
TextInputLayout_category.requestFocus();
}
flag=false;
}
XML代码段:
<android.support.design.widget.TextInputLayout
android:id="@+id/TextInputLayout_category"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.power2sme.android.utilities.customviews.BetterSpinner
android:id="@+id/Spinner_material"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/addrfq_hint_sku_category"
android:imeOptions="actionNext"
android:inputType="textNoSuggestions"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
BetterSpinner是第三方库,它源自AutoCompleteTextView。
答案 0 :(得分:0)
我将举一个在我的应用程序中运行的示例。
private boolean validatePassword() {
if (inputPassword.getText().toString().trim().isEmpty()) {
inputLayoutPassword.setErrorEnabled(true);
inputLayoutPassword.setError(getString(R.string.err_msg_password));
requestFocus(inputPassword);
return false;
} else {
inputLayoutPassword.setError(null);
inputLayoutPassword.setErrorEnabled(false);
}
return true;
}
private void requestFocus(View view) {
if (view.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
将我的requestFocus方法添加到您的代码中并尝试以下代码:
BetterSpinner Spinner_material = (BetterSpinner)rfqItem.findViewById(R.id.Spinner_material);
if(Spinner_material.getText()!=null && Spinner_material.getText().toString().length()>0)
{
TextInputLayout_category.setError(null);
TextInputLayout_category.setErrorEnabled(false);
}
else
{
if(isReporting)
{
TextInputLayout_category.setErrorEnabled(true);
TextInputLayout_category.setError(baseActivity.getString(R.string.addrfq_validationerror_material_category));
requestFocus(Spinner_material);
}
flag=false;
}
答案 1 :(得分:0)
请求视图焦点时:
private void requestFocus(View view) {
if (view.requestFocus()) {
yourcoordinatorlayout.clearFocus();
view.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
清除CoordinatorLayout
中的所有焦点时:
private void clearFocusFromAll() {
yourcoordinatorlayout.clearFocus();
}
在你身上Activity
声明CoordinatorLayout
赞:
CoordinatorLayout yourcoordinatorlayout=(CoordinatorLayout)findViewById(R.id.registerCoordinatorLayout);
xml就像:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/registerCoordinatorLayout"
tools:context=".activity.RegisterActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme3.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme3.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<!-- your views will be here, you can add nestedscrollview,layouts etc. -->
<include layout="@layout/content_register" />
</android.support.design.widget.CoordinatorLayout>