EditText中第一个字母后的键盘错误

时间:2018-09-27 23:59:29

标签: android android-studio android-edittext

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

代码和屏幕上:
Edit text before bug
Ghost keyboard(您看到的是它,但它不在那里,如果我单击“主页”按钮然后回来,则键盘消失了,我可以与按钮打交道并在其下编辑文本)

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" />

1 个答案:

答案 0 :(得分:0)

找到了解决方案,它与代码或android无关。我使用的是Xiomi手机,建议在编码时在开发人员选项中禁用MIUI优化。这是问题所在,重新启用它并重新启动电话即可解决问题。感谢您的回答!