Android:ScrollView中的EditText:隐藏键盘生成奇怪的滚动

时间:2017-01-02 08:56:04

标签: android android-edittext scrollview

我有这个布局示例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">

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

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:background="#FFF880"
            android:gravity="center_vertical"
            android:orientation="horizontal">

        </FrameLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ScrollView
                    android:id="@+id/scroll"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/action_bar_layout">

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

                        <TextView
                            android:id="@+id/emotion"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@android:color/holo_blue_bright" />

                        <EditText
                            android:id="@+id/edit"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@android:color/transparent"
                            android:inputType="textMultiLine|textCapSentences"
                            android:singleLine="false"
                            android:text="On sait depuis longtemps "
                            android:textColor="@android:color/black" />

                        <FrameLayout
                            android:id="@+id/photo"
                            android:layout_width="match_parent"
                            android:layout_height="350dp"
                            android:background="#EE00FF">

                        </FrameLayout>

                    </LinearLayout>

                </ScrollView>

                <FrameLayout
                    android:id="@+id/action_bar_layout"
                    android:layout_width="match_parent"
                    android:layout_height="56dp"
                    android:layout_alignParentBottom="true"
                    android:background="#990000"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">

                </FrameLayout>

            </RelativeLayout>

        </RelativeLayout>

    </LinearLayout>

</RelativeLayout>

我的样本活动:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edittext);
        final ScrollView scrollView = (ScrollView)findViewById(R.id.scroll);
        final EditText edittext = (EditText)findViewById(R.id.edit);

        // Setup scrollview
        scrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_MOVE:
                        // Hide keyboard
                        UIUtils.hideKeyboard(edittext, EditTextActivity.this);
                        break;
                }
                return false;
            }
        });

public static void hideKeyboard(View view, Activity activity) {
        if (view != null) {
            view.clearFocus();
            if (activity != null) {
                activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
                InputMethodManager imm = (InputMethodManager) activity.
                        getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }
    }

所以我想像Facebook新帖这样的行为:当你想滚动键盘时会自动消失。

但在我的情况下,当您想要在键盘隐藏时滚动时会出现奇怪的行为。

谢谢你们的帮助!

3 个答案:

答案 0 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/action_bar_layout"
    android:fillViewport="true">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:orientation="vertical">

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

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:background="#FFF880"
                android:gravity="center_vertical"
                android:orientation="horizontal">

            </FrameLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

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

                        <TextView
                            android:id="@+id/emotion"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@android:color/holo_blue_bright" />

                        <EditText
                            android:id="@+id/edit"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@android:color/transparent"
                            android:inputType="textMultiLine|textCapSentences"
                            android:singleLine="false"
                            android:text="On sait depuis longtemps "
                            android:textColor="@android:color/black" />

                        <FrameLayout
                            android:id="@+id/photo"
                            android:layout_width="match_parent"
                            android:layout_height="350dp"
                            android:background="#EE00FF">

                        </FrameLayout>

                    </LinearLayout>


                    <FrameLayout
                        android:id="@+id/action_bar_layout"
                        android:layout_width="match_parent"
                        android:layout_height="56dp"
                        android:layout_alignParentBottom="true"
                        android:background="#990000"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">

                    </FrameLayout>

                </RelativeLayout>

            </RelativeLayout>

        </LinearLayout>

    </RelativeLayout>
</ScrollView>

试着希望它能解决你的问题。

答案 1 :(得分:0)

您可以将键盘隐藏在编辑文本的外部触控上,而不是将键盘隐藏在滚动上

通过添加以下属性

,使父视图(活动的内容视图)可单击并可聚焦
    android:clickable="true" 
    android:focusableInTouchMode="true" 

实施hideKeyboard()方法

    public void hideKeyboard(View view) {
        InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

并设置edittext的onFocusChangeListener。

    edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                hideKeyboard(v);
            }
        }
    });

或者如果你想隐藏在滚动上,那么你可以添加主视图的触摸列表器而不是滚动视图

findViewById(R.id.mainLayout).setOnTouchListener(this)

答案 2 :(得分:0)

在主容器上设置android:focusableInTouchMode="true"