首页 > 解决方案 > EditText 中第一个字母后的键盘错误

问题描述

在我的第一个活动中,当我选择文本编辑时,键盘会出现。当我写一个字母时,页面滚动,就好像编辑文本失去了焦点(但它没有)并且键盘保持显示即使它不存在(如果我大约单击显示键盘下的编辑文本键盘入口动画重新开始,一切正常)。我不知道该怎么办,看起来对我来说真的像个虫子。哦,这个错误只出现在这个活动上(第一个活动开始),Android 8.1

代码和屏幕:在错误幽灵键盘
之前编辑文本(你看到它但它不存在,如果我点击主页按钮然后回来,键盘消失了,我可以与按钮交互并在LoginActivity.java 下编辑文本:



public class LoginActivity extends AppCompatActivity {

private EditText e;

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

    final Button b = findViewById(R.id.startButton);
    e = findViewById(R.id.name_input);

    //if i click button or enter get to nex activity
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            validate();
        }
    });

    e.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                validate();
                return true;
            }
            return false; // i don't know android that much, but i don't think there is a problem here
        }
    });

    //testing purpose
    e.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                Log.e("ERROR", "LOST FOCUS"); //Never called
            } else {
                Log.e("ERROR", "GOT FOCUS");
            }
        }
    });

    //For the ui style, commenting it changes nothing
    final View decorView = getWindow().getDecorView();

    final int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
    decorView.setSystemUiVisibility(uiOptions);

    decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
        @Override
        public void onSystemUiVisibilityChange(int visibility)  {
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                decorView.setSystemUiVisibility(uiOptions);
            }
        }
    });

}

@Override protected void onPause() {
    super.onPause();

}

@Override protected void onResume() {
    super.onResume();


}

private void validate() {
    if (!e.getText().toString().isEmpty() && e.getText().toString().length() < 16) {
        SharedInfos.setName(e.getText().toString());
        Log.e("Name : ", e.getText().toString());

        Intent activityLaunch = new Intent(this, PresentationActivity.class);
        this.startActivity(activityLaunch);
    } else {
        Toast.makeText(LoginActivity.this, "Name must be not empty and less than 16 char long !", Toast.LENGTH_LONG).show();
    }
}

}



activity_main.xml(布局)

<android.support.constraint.ConstraintLayout 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"
tools:context=".LoginActivity">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="343dp"
    android:layout_height="336dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:contentDescription="Cat"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_launcher_foreground" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:fontFamily="@font/carter_one"
    android:text="@string/app_name"
    android:textColor="@color/accent_material_dark_1"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="@+id/imageView"
    app:layout_constraintEnd_toEndOf="@+id/imageView"
    app:layout_constraintStart_toStartOf="@+id/imageView"
    app:layout_constraintTop_toTopOf="@+id/imageView"
    app:layout_constraintVertical_bias="0.9" />

<Button
    android:id="@+id/startButton"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="32dp"
    android:fontFamily="@font/carter_one"
    android:text="@string/app_then"
    android:textColor="@color/accent_material_dark_1"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="@+id/name_input"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@+id/name_input" />

<EditText
    android:id="@+id/name_input"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="8dp"
    android:ems="10"
    android:fontFamily="@font/carter_one"
    android:hint="@string/field_name"
    android:inputType="text|textNoSuggestions|textVisiblePassword"
    android:textColor="@color/accent_material_dark_1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/startButton"
    app:layout_constraintHorizontal_bias="0.51"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView" />

标签: androidandroid-edittext

解决方案


找到解决方案,它与代码或android无关。我正在使用小米手机,在互联网上建议在编码时禁用开发选项中的 MIUI 优化。这就是问题所在,重新启用它并重新启动手机即可解决问题。感谢您的回答!


推荐阅读