如何让EditText占用所需的空间,可以与上面的内容一起滚动?

and*_*per 26 android android-edittext android-nestedscrollview

背景

我有一个在顶部有一些视图的布局,它应该可以与它们下面的EditText一起滚动.

EditText占用了剩余的空间,占用了所需的空间.

这是一个演示它的示例POC布局(此处仅使用了2个EditTexts):

<android.support.v4.widget.NestedScrollView android:id="@+id/nestedScrollView"
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent" android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">

        <EditText
            android:id="@+id/titleEditText" android:layout_width="match_parent" android:layout_height="wrap_content"
            android:ellipsize="end" android:hint="title" android:imeOptions="actionNext|flagNoExtractUi"
            android:inputType="text|textAutoCorrect|textCapSentences" android:maxLines="1"
            android:nextFocusDown="@id/contentEditText" android:nextFocusForward="@id/contentEditText"
            android:scrollHorizontally="true" android:textColor="#2a2f3b" android:textColorHint="#a3a3a3"
            android:textSize="21sp"/>

        <EditText
            android:id="@+id/contentEditText" android:layout_width="match_parent" android:layout_height="match_parent"
            android:gravity="top" android:hint="content" android:background="@android:drawable/alert_light_frame"
            android:imeOptions="actionDone|flagNoEnterAction|flagNoExtractUi" android:textSize="18sp"
            android:inputType="textMultiLine|textAutoCorrect|textCapSentences"/>

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

我已经设置了一个背景框架,可以直观地显示EditText的大小.

问题

我已经找到了很多我写的解决方案,但是没有一个能够很好地处理滚动.

我一直看到的,至少是其中一个问题:

  1. 无法滚动整个页面(只有EditText可以滚动,我试图避免),所以无法再进入顶部的视图.
  2. 当我输入文本时,插入符号可能会出现在可见区域之外
  3. 当我输入越来越多的行时,它不会滚动整个页面.仅在EditText本身.

我试过的

我尝试过这些解决方案:

  1. 所有这里,这里,这里,这里.也许更多,但我没有保持足够的轨道......
  2. windowSoftInputMode在清单中尝试了各种值,并尝试isNestedScrollingEnabled在NestedScrollView中进行设置.
  3. 尝试在XML中进行各种配置,让EditText占用所需的空间,以防止它在其中滚动.

这个问题

如何让底部的EditText占用尽可能多的空间,并且仍然可以滚动整个NestedScrollView,而不会出现编辑问题?


编辑:因为原来的应用程序有点复杂,底部有一些视图(在工具栏内部)当你没有专注于底部的EditText时自动隐藏,这就得到了我找不到的答案上班.

此外,我不小心将赏金给予错误的答案,所以这是一个新的赏金,在更复杂的POC上.问题保持不变.NestedScrollView应该保持在同一个地方,而不是在关注底部EditText时滚动.

<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:orientation="vertical">

    <View
        android:layout_width="0dp" android:layout_height="0dp" android:focusable="true"
        android:focusableInTouchMode="true"/>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScrollView" android:layout_width="match_parent" android:layout_height="0px"
        android:layout_weight="1" android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">

            <EditText
                android:id="@+id/titleEditText" android:layout_width="match_parent" android:layout_height="wrap_content"
                android:ellipsize="end" android:hint="title" android:imeOptions="actionNext|flagNoExtractUi"
                android:inputType="text|textAutoCorrect|textCapSentences" android:maxLines="1"
                android:nextFocusDown="@id/contentEditText" android:nextFocusForward="@id/contentEditText"
                android:scrollHorizontally="true" android:textColor="#2a2f3b" android:textColorHint="#a3a3a3"
                android:textSize="21sp"/>

            <android.support.constraint.ConstraintLayout
                android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"
                android:background="@android:drawable/alert_light_frame" android:clickable="true"
                android:focusable="false">

                <EditText
                    android:id="@+id/contentEditText" android:layout_width="match_parent"
                    android:layout_height="wrap_content" android:background="@null" android:gravity="top"
                    android:hint="content" android:imeOptions="actionDone|flagNoEnterAction|flagNoExtractUi"
                    android:inputType="textMultiLine|textAutoCorrect|textCapSentences" android:textSize="18sp"/>
            </android.support.constraint.ConstraintLayout>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <LinearLayout
        android:layout_width="match_parent" android:layout_height="wrap_content" android:animateLayoutChanges="true"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/autoHideLayout" android:layout_width="match_parent" android:layout_height="wrap_content"
            android:orientation="horizontal" android:visibility="gone" tools:visibility="visible">

            <Button
                android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button"/>

            <Button
                android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button2"/>

        </LinearLayout>
    </LinearLayout>
</LinearLayout>


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        container.setOnClickListener {
            contentEditText.requestFocus()
            contentEditText.setSelection(contentEditText.length())
        }
        contentEditText.setOnFocusChangeListener { view, hasFocus ->
            autoHideLayout.visibility = if (hasFocus) View.VISIBLE else View.GONE
            if (hasFocus)
                nestedScrollView.scrollTo(0, 0)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

and*_*per 15

我得到了一些解决方法,通过将底部的EditText包装为一个可以使焦点重新布局的布局.

根本不需要太多代码

activity_main.xml中

<android.support.v4.widget.NestedScrollView
    android:id="@+id/nestedScrollView" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">

        <EditText
            android:id="@+id/titleEditText" android:layout_width="match_parent" android:layout_height="wrap_content"
            android:ellipsize="end" android:hint="title" android:imeOptions="actionNext|flagNoExtractUi"
            android:inputType="text|textAutoCorrect|textCapSentences" android:maxLines="1"
            android:nextFocusDown="@id/contentEditText" android:nextFocusForward="@id/contentEditText"
            android:scrollHorizontally="true" android:textColor="#2a2f3b" android:textColorHint="#a3a3a3"
            android:textSize="21sp"/>

        <android.support.constraint.ConstraintLayout
            android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"
            android:background="@android:drawable/alert_light_frame" android:clickable="true" android:focusable="false">

            <EditText
                android:id="@+id/contentEditText" android:layout_width="match_parent"
                android:layout_height="wrap_content" android:background="@null" android:gravity="top"
                android:hint="content" android:imeOptions="actionDone|flagNoEnterAction|flagNoExtractUi"
                android:inputType="textMultiLine|textAutoCorrect|textCapSentences" android:textSize="18sp"/>
        </android.support.constraint.ConstraintLayout>

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        container.setOnClickListener {
            contentEditText.requestFocus()
            contentEditText.setSelection(contentEditText.length())
        }
        contentEditText.setOnFocusChangeListener { view, hasFocus ->
            if (hasFocus) {
                nestedScrollView.scrollTo(0, 0)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

表现

<manifest package="com.example.user.myapplication" xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)


Dav*_*e S 13

您应该能够通过根据minLines屏幕高度计算出您想要的效果.请参阅下面的示例活动和布局.

您需要键入相当多的文本来使用最小行并超出屏幕高度以开始滚动行为,但您可以通过在minLines计算中添加一些常量行来避免这种情况

public class ScrollingActivity extends AppCompatActivity
{
    EditText editText2;

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

        editText2 = findViewById(R.id.editText2);

        int minHeight = getResources().getDisplayMetrics().heightPixels - editText2.getTop();
        float lineHeight = editText2.getPaint().getFontMetrics().bottom - editText2.getPaint().getFontMetrics().top;
        int minLines = (int)(minHeight/lineHeight);
        editText2.setMinLines(minLines);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    android:id="@+id/parentLayout"
    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="com.my.package.ScrollingActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000">
        <android.support.constraint.ConstraintLayout
            android:id="@+id/scrollingLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <EditText
                android:id="@+id/editText1"
                android:layout_margin="15dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent"
                android:background="#FFFFFF"/>
            <EditText
                android:id="@+id/editText2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@id/editText1"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_margin="15dp"
                android:background="#FFFFFF"/>
        </android.support.constraint.ConstraintLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

编辑

我重新创建了您的解决方案,您可以通过将此侦听器设置为EditText来纠正焦点问题.它通过在编辑文本获得焦点时覆盖滚动操作来工作,仅滚动到足以使光标可见.

contentEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if(hasFocus){
            nestedScrollView.scrollTo(0, 0);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

编辑2

我已经更新了我的答案,以反映新赏金的变化,这应该是非常接近你所需要的,如果我正确地理解这个问题.

public class ScrollingActivity extends AppCompatActivity
{
    ConstraintLayout parentLayout;
    EditText contentEditText;
    NestedScrollView nestedScrollView;
    LinearLayout autoHideLayout;

    boolean preventScroll = true;

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

        contentEditText = findViewById(R.id.contentEditText);
        nestedScrollView = findViewById(R.id.nestedScrollView);
        autoHideLayout = findViewById(R.id.autoHideLayout);
        parentLayout = findViewById(R.id.parentLayout);
        nestedScrollView.setOverScrollMode(View.OVER_SCROLL_NEVER);

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);


        parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int minHeight = autoHideLayout.getTop() - contentEditText.getTop();
                float lineHeight = contentEditText.getPaint().getFontMetrics().bottom - contentEditText.getPaint().getFontMetrics().top;
                int minLines = (int)(minHeight/lineHeight);
                if(minLines != contentEditText.getMinLines()){
                    contentEditText.setMinLines(minLines);
                }
            }
        });


        contentEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                ViewGroup.LayoutParams layoutParams = autoHideLayout.getLayoutParams();
                if(hasFocus){
                    nestedScrollView.scrollTo(0,0);
                    layoutParams.height = ConstraintLayout.LayoutParams.WRAP_CONTENT;
                } else{
                    layoutParams.height = 0;
                }
                autoHideLayout.setLayoutParams(layoutParams);
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

这是新的布局

    <android.support.constraint.ConstraintLayout
    android:id="@+id/parentLayout"
    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:orientation="vertical"
    android:animateLayoutChanges="true">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:focusable="false"
        android:focusableInTouchMode="false"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/autoHideLayout"
        >

        <LinearLayout
            android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

            <EditText
                android:id="@+id/titleEditText" android:layout_width="match_parent" android:layout_height="wrap_content"
                android:ellipsize="end" android:hint="title" android:imeOptions="actionNext|flagNoExtractUi"
                android:inputType="text|textAutoCorrect|textCapSentences" android:maxLines="1"
                android:nextFocusDown="@id/contentEditText" android:nextFocusForward="@id/contentEditText"
                android:scrollHorizontally="true" android:textColor="#2a2f3b" android:textColorHint="#a3a3a3"
                android:background="@android:drawable/alert_light_frame"
                android:textSize="21sp"/>

            <EditText
                android:id="@+id/contentEditText" android:layout_width="match_parent"
                android:layout_height="wrap_content" android:background="@android:drawable/alert_light_frame" android:gravity="top"
                android:hint="content" android:imeOptions="actionDone|flagNoEnterAction|flagNoExtractUi"
                android:inputType="textMultiLine|textAutoCorrect|textCapSentences" android:textSize="18sp"/>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <LinearLayout
        android:id="@+id/autoHideLayout" android:layout_width="0dp" android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:orientation="horizontal" android:visibility="visible" tools:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent">

        <Button
            android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button"/>

        <Button
            android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button2"/>

    </LinearLayout>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)


Sag*_*gar 3

通过更改layout.xml和MainActivity可以达到您预期的结果。更改layout_height并添加layout_weight如下ConstraintLayout

    <?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:orientation="vertical">

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:focusable="true"
        android:focusableInTouchMode="true" />

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <EditText
                android:id="@+id/titleEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:hint="title"
                android:imeOptions="actionNext|flagNoExtractUi"
                android:inputType="text|textAutoCorrect|textCapSentences"
                android:maxLines="1"
                android:nextFocusDown="@id/contentEditText"
                android:nextFocusForward="@id/contentEditText"
                android:scrollHorizontally="true"
                android:textColor="#2a2f3b"
                android:textColorHint="#a3a3a3"
                android:textSize="21sp" />

            <android.support.constraint.ConstraintLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@android:drawable/alert_light_frame"
                android:clickable="true"
                android:focusable="false"
                android:nestedScrollingEnabled="false">

<!-- -->
                <EditText
                    android:id="@+id/contentEditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@null"
                    android:gravity="top"
                    android:hint="content"
                    android:imeOptions="actionDone|flagNoEnterAction|flagNoExtractUi"
                    android:inputType="textMultiLine|textAutoCorrect|textCapSentences"
                    android:textSize="18sp" />
            </android.support.constraint.ConstraintLayout>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/autoHideLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:visibility="visible"
            tools:visibility="visible">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="button" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="button2" />

        </LinearLayout>
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Manifest.xml 是:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</application>
Run Code Online (Sandbox Code Playgroud)

记录scrollX和scroll Y的当前值并进行如下NestedScrollView调整:NestedScrollView

MainActivity.java

public class MainActivity extends AppCompatActivity {
   private int scrollX;
   private int scrollY;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       final EditText editText = findViewById(R.id.contentEditText);
       final LinearLayout autoHideLayout = findViewById(R.id.autoHideLayout);

       ConstraintLayout container = findViewById(R.id.container);
       container.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               autoHideLayout.setVisibility(View.VISIBLE);
               editText.requestFocus();
               editText.setSelection(editText.length());
           }
       });
       final NestedScrollView nestedScrollView = findViewById(R.id.nestedScrollView);
       editText.setOnTouchListener(new View.OnTouchListener() {
           @Override
           public boolean onTouch(View v, MotionEvent event) {
               scrollX = nestedScrollView.getScrollX();
               scrollY = nestedScrollView.getScrollY();
               autoHideLayout.setVisibility(View.VISIBLE);
               return false;
           }
       });
       editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
           @Override
           public void onFocusChange(View v, boolean hasFocus) {

               if (hasFocus)
                   nestedScrollView.scrollTo(scrollX, scrollY);
               if (!hasFocus) {
                   autoHideLayout.setVisibility(View.GONE);
               }
           }
       });
   }
}
Run Code Online (Sandbox Code Playgroud)

不需要时它不会滚动。看截图: 截屏