Android EditText焦点问题,错误的EditText

时间:2015-12-11 10:45:36

标签: android android-layout

EditText让我们疯狂!真的!

我们的应用登录界面存在问题。它包含两个EditTexts,但只有第一个(用户名)被聚焦,通过SoftKeyboard只显示触摸/点击用户名EditText。 一旦点击用户名EditText,因此SoftKeyboard可见,然后触摸/单击密码EditText它显示焦点(光标在密码EditText中闪烁),但键盘输入将插入用户名EditText。 / p>

现在有趣的部分。我们试图通过屏幕(通过按下电源按钮)将其关闭再打开,然后,出乎意料的是,一切都按预期工作。

我们错过了什么吗?

我们尝试了很多东西:

  • 展平了视图层次结构
  • 仅包含一个包含视图的线性布局的布局
  • 可聚焦,可在触摸模式下专注
  • 从父ScrollView中删除填充视口
  • 将片段转换为活动

我们正在使用Android设计支持库v23.1.1和AppCompat Library v23.1.1

Screenshot of the login screen

LoginFragment.java

public class LoginFragment extends Fragment {

private LoginButton fbLoginButton;
private EditText etxUsername;
private EditText etxPassword;
private Button btnLogin;
private Button btnRegister;
private Button btnRequestPassword;
private ImageView backgroundImage;
private View content;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup   container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_login, container, false);
        fbLoginButton = (LoginButton) root.findViewById(R.id.fb_login);
        fbLoginButton.setFragment(this);
        content = root.findViewById(R.id.content);
        etxUsername = (EditText) root.findViewById(R.id.etx_username);
        etxPassword = (EditText) root.findViewById(R.id.etx_password);
        btnLogin = (Button) root.findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(v -> login());
        btnRegister = (Button) root.findViewById(R.id.btn_register);
        btnRegister.setOnClickListener(v -> showRegister());
        btnRequestPassword = (Button) root.findViewById(R.id.btn_request_password);
        btnRequestPassword.setOnClickListener(v -> requestPassword());
        backgroundImage = (ImageView) root.findViewById(R.id.backgroundImage);
        return root;
    }
}

fragment_login.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <ImageView
        android:id="@+id/backgroundImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:scaleType="centerCrop"/>

    <ScrollView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:layout_centerInParent="true"
        android:background="@color/transparent_black">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <LinearLayout
                android:id="@+id/login_form"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    android:layout_gravity="center"
                    android:layout_marginBottom="@dimen/activity_vertical_margin"
                    android:padding="@dimen/activity_horizontal_margin"
                    android:src="@drawable/login_logo"/>

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:minHeight="48dp"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="24dp"
                        android:layout_height="24dp"
                        android:layout_marginRight="32dp"
                        android:src="@drawable/ic_w_user_24"/>

                    <EditText
                        android:id="@+id/etx_username"
                        style="@style/AccountManagerEditText.Light"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/username_or_email"
                        android:focusable="true"
                        android:nextFocusDown="@+id/etx_password"
                        android:inputType="text"/>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:minHeight="48dp"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="24dp"
                        android:layout_height="24dp"
                        android:layout_marginRight="32dp"
                        android:src="@drawable/ic_w_password_24"/>

                    <EditText
                        android:id="@+id/etx_password"
                        style="@style/AccountManagerEditText.Light"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:focusable="true"
                        android:hint="@string/password"
                        android:inputType="textPassword"/>
                </LinearLayout>
            </LinearLayout>

            <Button
                android:id="@+id/btn_request_password"
                style="@style/VLButtonFlat.Light"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/spacing"
                android:text="@string/forgot_password"
                android:textAllCaps="false"
                android:textAppearance="@style/TextAppearance.AppCompat.Caption"
                android:textColor="@color/white"/>

            <Button
                android:id="@+id/btn_login"
                style="@style/VLButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/login"/>


            <TextView
                style="@style/TextAppearance.AppCompat.Body1"
                android:layout_width="wrap_content"
                android:layout_height="38dp"
                android:autoLink="all"
                android:clickable="true"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="or"
                android:textColor="@color/white"/>

            <com.facebook.login.widget.LoginButton
                android:id="@+id/fb_login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="@dimen/activity_vertical_margin"
                android:layout_marginTop="@dimen/activity_vertical_margin"/>

            <Button
                android:id="@+id/btn_register"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="@string/create_new_account"
                android:textAllCaps="false"
                android:textAppearance="@style/TextAppearance.AppCompat.Caption"/>

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

编辑: 添加了样式

<resources
xmlns:tools=" http://schemas.android.com/tools"
tools:ignore="MissingPrefix">

<style name="AccountManagerTheme.Dark" parent="Theme.AppCompat">
    <item name="colorPrimary">@color/black</item>
    <item name="colorPrimaryDark">@color/black</item>
    <item name="colorAccent">@color/dirty_white</item>
    <item name="android:textColorPrimary">@color/black</item>
    <item name="android:textColorSecondary">@color/dirty_white</item>
</style>

<style name="AccountManagerEditText.Light" parent="Base.Widget.AppCompat.EditText">
    <item name="android:theme">@style/AccountManagerTheme</item>
    <item name="android:textAppearance">@style/TextAppearance.AppCompat.Body1</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:minHeight">48dp</item>
    <item name="android:textColorHint">@color/dirty_white</item>
</style>

<style name="AccountManagerEditText.Dark" parent="Base.Widget.AppCompat.EditText">
    <item name="android:theme">@style/AccountManagerTheme.Dark</item>
    <item name="android:textAppearance">@style/TextAppearance.AppCompat.Body1</item>
    <item name="android:textColorHint">@color/dirty_white</item>
    <item name="android:minHeight">48dp</item>
</style>

1 个答案:

答案 0 :(得分:0)

尝试在您的布局中将这两者合并为ScrollView的子代: 机器人:descendantFocusability =&#34; beforeDescendants&#34; 机器人:focusableInTouchMode =&#34;真&#34;