屏幕旋转更改后,AppCompatEditText会获得焦点

时间:2018-01-04 16:21:18

标签: android material-design

这是我的登录页面布局,所以当我从纵向模式旋转到横向或相反时,AppCompatEditText会聚焦

<RelativeLayout 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:focusable="true"
    android:focusableInTouchMode="true"
    android:id="@+id/loginLayout"
    tools:context="com.jovan.matetracker.LoginActivity">

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/usernameLayout"
    android:layout_marginTop="170dp"
    android:layout_marginRight="32dp"
    android:layout_marginLeft="32dp"
    android:layout_marginBottom="8dp">

    <android.support.v7.widget.AppCompatEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/username"
        android:hint="@string/username"
        android:inputType="text"/>

</android.support.design.widget.TextInputLayout>

我尝试将clearFocus()属性设置为onResume()方法内的View,但它不起作用。

更新

public class LoginActivity extends AppCompatActivity {
    RelativeLayout loginLayout;
    TextInputLayout usernameLayout;
    AppCompatEditText username;


    public void goToRegister(View view) {
        Intent intent = new Intent(getApplicationContext(),RegisterActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        loginLayout = findViewById(R.id.loginLayout);
        usernameLayout = findViewById(R.id.usernameLayout);
        username = findViewById(R.id.username);

        username.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {

                if(username.getText().toString().isEmpty()) {
                    usernameLayout.setErrorEnabled(true);
                    usernameLayout.setError("Please enter your username!");
                    loginButton.setEnabled(false);
                } else {
                    usernameLayout.setErrorEnabled(false);
                    loginButton.setEnabled(true);
                }

            }
        });

        username.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(username.getText().toString().isEmpty()) {
                    usernameLayout.setErrorEnabled(true);
                    usernameLayout.setError("Please enter your username!");
                    loginButton.setEnabled(false);
                } else {
                    usernameLayout.setErrorEnabled(false);
                    loginButton.setEnabled(true);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

首先我做的是注释onFocusChanged中的if语句,但我的用户名布局中仍然存在该错误。然后我评论了onTextChanged中的if statemet,并且在旋转之后我没有在TextInputLayout中出现错误和错误消息。

修复

经过几个小时的头痛后,我找到了解决问题的方法。这是解决方案。我在onTextChanged()方法中添加了一个if语句

`if((savedInstanceState != null) && (confirmPassword.getText().toString().isEmpty())) {
                    confirmPasswordLayout.setErrorEnabled(false);
                    registerButton.setEnabled(true);
                }

` 这是完整的onTextChanged()方法

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                if((savedInstanceState != null) && (confirmPassword.getText().toString().isEmpty())) {
                    confirmPasswordLayout.setErrorEnabled(false);
                    registerButton.setEnabled(true);
                } else if(confirmPassword.getText().toString().isEmpty()) {
                    confirmPasswordLayout.setErrorEnabled(true);
                    confirmPasswordLayout.setError("Please confirm your password!");
                    registerButton.setEnabled(false);
                } else {
                    confirmPasswordLayout.setErrorEnabled(false);
                    registerButton.setEnabled(true);
                }
            }

1 个答案:

答案 0 :(得分:0)

如果您不想在视图中使用自动对焦,请尝试在布局中添加:

<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
    android:focusable="true" 
    android:focusableInTouchMode="true"
    android:layout_width="0px" 
    android:layout_height="0px"/>

<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView 
    android:id="@+id/autotext"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:nextFocusUp="@id/autotext" 
    android:nextFocusLeft="@id/autotext"/>

来源:Stop EditText from gaining focus at Activity startup