显示对话框后,EditText自动滚动不起作用

时间:2019-08-11 17:15:45

标签: java android android-fragments android-edittext

我正在开发一个供用户共享其图书的应用程序。为了实现这一目标,我从用户那里获得了关于本书的不同数据。问题是,在显示和关闭Dialog之前,所有EditText的水平自动滚动都可以正常工作。一旦取消了片段上的对话框,就无法在此布局上水平自动滚动EditText。

我添加了:

android:focusable="true"
android:focusableInTouchMode="true"

编辑文本的父级布局,但不起作用。

另外,为了确保启用了自动滚动功能,我添加了:

android:scrollHorizontally="true"

但以上方法均无济于事。

这是片段布局的XML代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="@color/white"
    android:orientation="vertical"
    android:paddingStart="@dimen/dp25"
    android:paddingEnd="@dimen/dp25"
    tools:context=".UI.Fragments.SharePostFragments.Fragment1.OverViewFragment">

    <LinearLayout
        android:id="@+id/root_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:orientation="vertical">

        <EditText
            android:id="@+id/edit_text_name_of_book"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:fontFamily="@font/segoe_ui_semi_bold"
            android:hint="@string/name_of_book"
            android:inputType="text"
            android:maxLength="50"
            android:maxLines="1"
            android:scrollHorizontally="true"
            android:textAlignment="textStart"
            android:textColor="@color/colorPrimaryDark"
            android:textColorHint="@color/colorPrimaryDark"
            android:textSize="@dimen/font22" />

        <TextView
            android:id="@+id/text_view_number_of_characters"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_marginEnd="@dimen/dp10"
            android:fontFamily="@font/segoe_ui_light"
            android:text="@string/_0_50"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/font16"
            tools:ignore="RtlSymmetry" />

        <EditText
            android:id="@+id/edit_text_name_of_author"
            android:layout_width="match_parent"
            android:layout_height="@dimen/textBoxHeight"
            android:layout_marginTop="@dimen/dp30"
            android:background="@drawable/round_text_box_gray"
            android:fontFamily="@font/segoe_ui_regular"
            android:hint="@string/name_of_writer"
            android:inputType="text"
            android:paddingStart="20dp"
            android:paddingEnd="20dp"
            android:textColor="@color/colorPrimaryDark"
            android:textColorHint="@color/colorPrimaryDark"
            android:textSize="@dimen/font16" />

        <RelativeLayout
            android:id="@+id/constraint_layout_1"
            android:layout_width="match_parent"
            android:layout_height="@dimen/textBoxHeight"
            android:layout_marginTop="@dimen/dp20"
            android:background="@drawable/round_text_box_border_gray"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_marginTop="3dp"
                android:fontFamily="@font/segoe_ui_regular"
                android:paddingStart="@dimen/dp20"
                android:paddingEnd="@dimen/dp20"
                android:text="@string/price_of_book"
                android:textColor="@color/colorPrimaryDark"
                android:textSize="@dimen/font16" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:paddingStart="@dimen/dp20"
                android:paddingEnd="@dimen/dp20"
                android:text="@string/azn_sign"
                android:textColor="@color/colorPrimaryDark"
                android:textSize="@dimen/font16" />

            <EditText
                android:id="@+id/edit_text_price_of_book"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toStartOf="@+id/textView2"
                android:background="@color/white"
                android:clickable="false"
                android:hint="@string/_0_0"
                android:inputType="text|numberDecimal"
                android:maxLength="6"
                android:singleLine="true"
                android:textColorHint="@android:color/black" />

        </RelativeLayout>

        <LinearLayout
            android:id="@+id/constraint_layout_conditions_root"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp20"
            android:background="@drawable/round_text_box_border_gray"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/linear_layout_1"
                android:layout_width="match_parent"
                android:layout_height="@dimen/textBoxHeight"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="9"
                    android:gravity="center_vertical"
                    android:paddingStart="@dimen/dp20"
                    android:paddingEnd="@dimen/dp20"
                    android:text="@string/book_condition_placeholder"
                    android:textColor="@color/colorPrimaryDark" />

                <ImageButton
                    android:id="@+id/image_button_conditions"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginEnd="@dimen/dp10"
                    android:layout_weight="1"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:src="@drawable/ic_spinner" />

            </LinearLayout>

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

                <TextView
                    android:id="@+id/text_view_new"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingStart="@dimen/dp20"
                    android:paddingTop="@dimen/dp10"
                    android:paddingEnd="@dimen/dp20"
                    android:paddingBottom="@dimen/dp10"
                    android:text="@string/_new"
                    android:textColor="@color/dark_gray_text_color" />

                <TextView
                    android:id="@+id/text_view_normal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingStart="@dimen/dp20"
                    android:paddingTop="@dimen/dp10"
                    android:paddingEnd="@dimen/dp20"
                    android:paddingBottom="@dimen/dp10"
                    android:text="@string/normal"
                    android:textColor="@color/dark_gray_text_color" />

                <TextView
                    android:id="@+id/text_view_old"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingStart="@dimen/dp20"
                    android:paddingTop="@dimen/dp10"
                    android:paddingEnd="@dimen/dp20"
                    android:paddingBottom="@dimen/dp10"
                    android:text="@string/old"
                    android:textColor="@color/dark_gray_text_color" />

            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/constraint_layout_languages_root"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp20"
            android:background="@drawable/round_text_box_border_gray"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/linear_layout_2"
                android:layout_width="match_parent"
                android:layout_height="@dimen/textBoxHeight"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="9"
                    android:gravity="center_vertical"
                    android:paddingStart="@dimen/dp20"
                    android:paddingEnd="@dimen/dp20"
                    android:text="@string/language"
                    android:textColor="@color/colorPrimaryDark" />

                <ImageButton
                    android:id="@+id/image_button_languages"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginEnd="@dimen/dp10"
                    android:layout_weight="1"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:src="@drawable/ic_spinner" />

            </LinearLayout>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view_languages"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingStart="@dimen/dp20"
                android:paddingEnd="@dimen/dp20"
                android:paddingBottom="@dimen/dp10"
                android:visibility="gone" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

片段Java代码:

package org.kitapp.UI.Fragments.SharePostFragments.Fragment1;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import org.kitapp.R;
import org.kitapp.UI.Dialogs.ConditionDialog.ConditionOfBookDialog;
import org.kitapp.UI.Dialogs.LanguageDialog.LanguageDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;

public class OverViewFragment extends Fragment implements OverViewContractor.View {

    private final String TAG = OverViewFragment.class.getSimpleName();

    private Context mContext;
    private OverViewCallback mListener;

    private OverViewPresenter mPresenter;

    @BindView(R.id.constraint_layout_conditions_root)
    LinearLayout conditionRoot;

    @BindView(R.id.constraint_layout_languages_root)
    LinearLayout languagesRoot;

    @BindView(R.id.linear_layout_conditions)
    LinearLayout conditions;

    @BindView(R.id.root_layout)
    LinearLayout rootLayout;

    @BindView(R.id.text_view_number_of_characters)
    TextView numberOfChars;

    @BindView(R.id.edit_text_name_of_book)
    EditText nameOfBook;

    public interface OverViewCallback {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_over_view, container, false);
        ButterKnife.bind(this, view);

        new OverViewPresenter(this);

        //rootLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);

        return view;
    }

    ////// CONTRACTOR METHODS //////

    @Override
    public void setPresenter(OverViewPresenter presenter) {
        this.mPresenter = presenter;
    }

    ////// LISTENERS //////

    @Override
    @OnClick(R.id.image_button_conditions)
    public void showConds() {
        ConditionOfBookDialog conditionOfBookDialog = new ConditionOfBookDialog(mContext);
        conditionOfBookDialog.show();
    }

    @Override
    @OnClick(R.id.image_button_languages)
    public void showLangs() {
        LanguageDialog languageDialog = new LanguageDialog(mContext);
        languageDialog.show();
    }

    @Override
    @SuppressLint("SetTextI18n")
    @OnTextChanged(R.id.edit_text_name_of_book)
    public void onNameOfBookChange() {
        int len = nameOfBook.getText().toString().trim().length();
        numberOfChars.setText(len + "/50");
        if (len > 50) {
            nameOfBook.setText(nameOfBook.getText().toString().substring(0, 50));
            nameOfBook.setSelection(nameOfBook.getText().length());
        }
    }

    ////// FRAGMENT METHODS //////

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        this.mContext = context;

        if (context instanceof OverViewCallback) {
            mListener = (OverViewCallback) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OverViewCallback");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

}

UI Design and Dialog

在上图中,有EditTexts 书名作者名。在显示对话框之前,它们两个都像一个超级按钮。写东西时,光标自动滚动到末尾。显示并关闭Dialog后,它们都无法正常工作,光标停留在EditText的末尾,但仍在隐藏部分继续写入字符。

在该用户界面中由于该问题而无法看到它,但是我已经写了。 玫瑰是红色,紫罗兰是蓝色,Stackoverflow我爱你

但是只有玫瑰是红色,紫罗兰是蓝色,Stac

Weird buggy layout UI image

请帮我解决这个问题。预先感谢。

1 个答案:

答案 0 :(得分:0)

作为解决方案,我不知道其根本原因,但是一旦我从项目中删除了 Butterknife 库,问题就解决了。