简体   繁体   English

如何禁用软键盘但在android中的多行编辑文本中保持启用滚动条

[英]how to disable soft keyboard but keep enable scrollbar in multiline edittext in android

I have scrollbar in multiline edittext and when user tap on edittext soft keyboard opens.我在多行 edittext 中有滚动条,当用户点击 edittext 软键盘打开时。

I am able to disable softkeyboard but on same time scrolling is also disabled.我可以禁用软键盘,但同时也禁用了滚动。

I wants to stop opening soft keyboard but scrolling should be enable我想停止打开软键盘,但应该启用滚动

Below is my java code下面是我的java代码

 m_etNotes.setOnTouchListener(new View.OnTouchListener()
    {
        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View p_v, MotionEvent p_event)
        {

            if(m_etNotes != null && m_etNotes.getLineCount() > 4)
            {
                m_etNotes.getParent().getParent().requestDisallowInterceptTouchEvent(true);
                switch(p_event.getAction() & MotionEvent.ACTION_MASK)
                {
                    case MotionEvent.ACTION_UP:
                        m_etNotes.getParent().getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                }
            }
            return false;
        }
    });

Below is my xml code下面是我的xml代码

<EditText
android:id="@+id/anl_etNotes"
android:layout_width="fill_parent"
android:focusable="false"
android:enabled="false"
android:lines="4"
android:maxLines="4"
android:scrollbars="vertical" />

what code you are using to try to hide the keyboard ?您使用什么代码来尝试隐藏键盘?

Try this尝试这个

InputMethodManager inputManager;
inputManager = (InputMethodManager) this
                .getSystemService(Context.INPUT_METHOD_SERVICE);

//put the below line in the edit text so that it can hide the keyboard //将下面的行放在编辑文本中,以便它可以隐藏键盘

inputManager.hideSoftInputFromWindow(v.getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);

and use scrollView as a parent tag in your XML file并在 XML 文件中使用 scrollView 作为父标签

Use ScrollView as a parent tag try to make XML file like this:使用 ScrollView 作为父标签尝试制作这样的 XML 文件:

   <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#E0E0E0"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin" >


<EditText
android:id="@+id/anl_etNotes"
android:layout_width="fill_parent"
android:focusable="false"
android:enabled="false"
android:lines="4"
android:maxLines="4"
android:scrollbars="vertical" />

</RelativeLayout>

</ScrollView>

Use this on to hide soft keyboard when you touch out side of textView当您触摸 textView 的一侧时,使用此选项隐藏软键盘

public void setupUI(View view) {
    if (!(view instanceof EditText)) {
        view.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                hideSoftKeyboard(getActivity());
                return false;
            }
        });
    }
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View innerView = ((ViewGroup) view).getChildAt(i);
            setupUI(innerView);
        }
    }
}
private void hideSoftKeyboard(Activity activity) {
    InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM