簡體   English   中英

如何禁用軟鍵盤但在android中的多行編輯文本中保持啟用滾動條

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

我在多行 edittext 中有滾動條,當用戶點擊 edittext 軟鍵盤打開時。

我可以禁用軟鍵盤,但同時也禁用了滾動。

我想停止打開軟鍵盤,但應該啟用滾動

下面是我的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;
        }
    });

下面是我的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" />

您使用什么代碼來嘗試隱藏鍵盤?

嘗試這個

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

//將下面的行放在編輯文本中,以便它可以隱藏鍵盤

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

並在 XML 文件中使用 scrollView 作為父標簽

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

當您觸摸 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