如何在使用ScrollView时隐藏软键盘输入

时间:2015-01-08 19:37:42

标签: android scrollview android-softkeyboard

如何在ScrollView使用LinearLayout时隐藏软键盘输入?

我尝试在我的活动类中实现以下内容,但这些解决方案都没有产生预期的结果:

(1)

@Override
public boolean onTouchEvent(MotionEvent event) 
{
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    return true;
}

(2)

@Override
public boolean onTouchEvent(MotionEvent event) 
{
    ScrollView myScrollView = (ScrollView) findViewById(R.id.scrollview); //of course scrollview was id in layout then
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    return true;
}

(3)

与#2相同,但使用LinearLayout代替ScrollView


这三种解决方案都不适用于我。

我注意到的一件事是,当我从ScrollView文件中删除layout.xml时,一切都按预期工作。

4 个答案:

答案 0 :(得分:2)

就我而言,我正在生成一个动态表单,其中包含滚动视图中的太多edittext字段,并在滚动表单时隐藏键盘我尝试了太多选项但最终能够使用下面的代码进行修复:

scrollView.setOnTouchListener(new View.OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        if (event != null && event.getAction() == MotionEvent.ACTION_MOVE)
        {
            InputMethodManager imm = ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE));
            boolean isKeyboardUp = imm.isAcceptingText();

            if (isKeyboardUp)
            {
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        }
        return false;
    }
});

答案 1 :(得分:0)

ScrollView可能正在消耗触摸事件。也许尝试挂钩滚动视图的onFocusChangeListener并从那里隐藏键盘?

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

您可能还需要在xml的滚动视图中添加一些属性。

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

答案 2 :(得分:0)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_id_root);
    setHideKeyboard(this, layout);
}

public void setHideKeyboard(final Context context, View view) {
    try {
        //Set up touch listener for non-text box views to hide keyboard.
        if (!(view instanceof EditText || view instanceof ScrollView)) {

            view.setOnTouchListener(new View.OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {
                    InputMethodManager in = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                    in.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    return false;
                }

            });
        }

        //If a layout container, iterate over children and seed recursion.
        if (view instanceof ViewGroup) {

            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {

                View innerView = ((ViewGroup) view).getChildAt(i);

                setHideKeyboard(context, innerView);
            }
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}

答案 3 :(得分:0)

首先,扩展EditText并添加一段代码,以便在每次EditText实例失去焦点时帮助解除键盘

public class MyEditText extends AppCompatEditText {

public MyEditText(Context context) {
    super(context);
    setupEditText();
}

public MyEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    setupEditText();
}

public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setupEditText();
}

public void setupEditText() {
    // Any time edit text instances lose their focus, dismiss the keyboard!
    setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus && !(findFocus() instanceof MyEditText)) {
                hideKeyboard(v);
            } else {
                showKeyboard(v);
            }
        }
    });
}

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

public void showKeyboard(View view) {
    InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.showSoftInput(view, 0);
}
}

然后,在ScrollView的子布局中设置android:clickable="true"android:focusableInTouchMode="true"

请注意,它应该是ScrollView的子布局,而不是ScrollView本身。

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

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

    <MyEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    </MyEditText>

</LinearLayout>

</ScrollView>

这应该有用!