Scroll to scroll through the Enter key on the soft keyboard

I have a simple layout with nested elements in its xml inside ScrollView, its basically a form to get user input with several objects EditTextand others. One of mine EditTexthas an attribute

android:lines="2"

for the field the user’s address, and it should contain 2 lines. Therefore, when the user enters input in EditText, the Enter key is displayed on the soft keyboard and when you press this key again, it ScrollViewautomatically scrolls down each time you press Enter and EditText loses focus and the layout automatically scrolls down to it. How to stop it.

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/rel_main_footer"
    android:layout_below="@+id/enquiry_header" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tv_wit_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="@string/wit_head"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#7f0100" />
        <!-- Owner Details -->

        <LinearLayout
            android:id="@+id/li1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_wit_header"
            android:orientation="horizontal"
            android:padding="8dp" >

            <TextView
                android:id="@+id/tv_wit_owner"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:text="@string/wit_name"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <EditText
                android:id="@+id/et_wit_owner"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:background="#00000000"
                android:hint="@string/hint_set_owner"
                android:inputType="text"
                android:singleLine="true"
                android:textColor="#7f0000"
                android:textColorHint="#a65b5a" />
        </LinearLayout>

        <View
            android:id="@+id/v1"
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:layout_below="@+id/li1"
            android:background="#A4A4A4" />

        <LinearLayout
            android:id="@+id/li2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/v1"
            android:orientation="horizontal"
            android:padding="8dp" >

            <TextView
                android:id="@+id/tv_wit_address"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:text="@string/ud_address"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <EditText
                android:id="@+id/et_wit_address"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:background="#00000000"
                android:gravity="top|left"
                android:hint="@string/hint_set_address"
                android:lines="2"
                android:singleLine="true"
                android:scrollbars="vertical"
                android:textColor="#7f0000"
                android:textColorHint="#a65b5a" />
        </LinearLayout>

        <View
            android:id="@+id/v2"
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:layout_below="@+id/li2"
            android:background="#A4A4A4" />

        <LinearLayout
            android:id="@+id/li3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/v2"
            android:orientation="horizontal"
            android:padding="8dp" >

            <TextView
                android:id="@+id/tv_wit_number"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:text="@string/wit_number"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <EditText
                android:id="@+id/et_wit_number"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:background="#00000000"
                android:hint="@string/hint_wit_number"
                android:inputType="phone"
                android:singleLine="true"
                android:textColor="#7f0000"
                android:textColorHint="#a65b5a" />
        </LinearLayout>

        <View
            android:id="@+id/v3"
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:layout_below="@+id/li3"
            android:background="#A4A4A4" />

        <LinearLayout
            android:id="@+id/li4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/v3"
            android:orientation="vertical"
            android:padding="8dp" >

            <TextView
                android:id="@+id/tv_wit_notes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/wit_notes"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <EditText
                android:id="@+id/et_wit_notes"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:gravity="top|left"
                android:hint="@string/hint_wit_notes"
                android:inputType="textMultiLine"
                android:lines="5"
                android:maxLines="5"
                android:textColor="#7f0000"
                android:textColorHint="#a65b5a" />
        </LinearLayout>
    </RelativeLayout>
</ScrollView>
+4
source share
2

100% - , .

et_wit_address.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start,
                int before, int count) {
            scrollview.scrollTo(0, et_wit_number.getTop());
                        }

        @Override
        public void beforeTextChanged(CharSequence s, int start,
                int count, int after) {
            scrollview.scrollTo(0, et_wit_number.getTop());
        }

        @Override
        public void afterTextChanged(Editable s) {
            scrollview.scrollTo(0, et_wit_number.getTop());
        }
    });
+1

android: imeOptions .

ActionNext:

"" , , .

ActionDone

"done", , IME .

:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="16dp"
        android:imeOptions="actionNext"
        android:singleLine="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="24dp"
        android:imeOptions="actionDone"
        android:singleLine="true"
        android:ems="10" />

</RelativeLayout>
0

Source: https://habr.com/ru/post/1526278/


All Articles