Scrollview自动滚动输入软输入键盘的Enter键

时间:2014-02-11 03:18:17

标签: android android-layout

我有一个简单的布局,其嵌套元素位于ScrollView内的xml中,它基本上是一个用于输入多个EditText和其他对象的用户输入的表单。 我的EditText之一有属性

android:lines="2"

用户地址字段,必须有2行。因此,当用户输入EditText时,输入键将显示在软输入键盘上,并且在每次单击Enter时,重复按下此输入键main Scrollview自动滚动到底部,而EditText则失去焦点和布局自动向下滚动到底部。如何阻止这种情况发生。

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

2 个答案:

答案 0 :(得分:1)

对于您的问题,它不是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 :(得分:0)

您可以使用android:imeOptions来处理键盘上的额外按钮。

动作下一步:

操作键执行&#34; next&#34;操作,将用户带到将接受文本的下一个字段。

<强> ActionDone

操作键执行&#34;完成&#34;操作,通常意味着没有什么可以输入,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>