具有多行和adjustPan windowSoftInputMode的Android edittext

时间:2017-02-06 10:55:41

标签: android multiline window-soft-input-mode adjustpan

在我目前正在处理的聊天应用中,我有一个带编辑文字的底栏,这是我的editText布局:

 <RelativeLayout
    android:id="@+id/bottom_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
>

  <EditText
      android:id="@+id/edt_send_message"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_gravity="center_vertical"
      android:layout_marginStart="12dip"
      android:layout_toEndOf="@+id/img_add"
      android:layout_toStartOf="@+id/img_send_message"
      android:background="#00000000"
      android:hint="@string/send_message"
      android:inputType="textCapSentences|textMultiLine|textAutoComplete|textAutoCorrect"
      android:isScrollContainer="false"
      android:lineSpacingMultiplier="1.2"
      android:maxHeight="120dip"
      android:minHeight="45dip"
      android:paddingBottom="19dip"
      android:paddingTop="19dip"
      android:scrollbars="none"
      android:textColorHint="@color/colorPrimary"
      android:textSize="16sp">
  </EditText>

<!-- OTHER STUFF -->

</RelativeLayout>

当我开始输入时很好,多线踢得很好。

但是当我将焦点移动到第一行时,会发生这种情况,编辑文本的一部分移动到键盘下方。

enter image description here

我知道使用 adjustResize 可以解决这个问题,但我不能使用它,因为我有一个位图并且它会被压缩。所以我不能使用 adjustResize

需要帮助,提前致谢。

2 个答案:

答案 0 :(得分:0)

有趣的问题,我唯一想到的就是这个hacky解决方案。此外,我不确定200ms对于较慢的设备是否足够。但也许它会帮助你。

final EditText txtEdit = (EditText) findViewById(R.id.edt_send_message);
        txtEdit.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                final int sel = txtEdit.getSelectionStart();
                txtEdit.setSelection(txtEdit.getText().length());
                txtEdit.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        txtEdit.setSelection(sel);
                    }
                }, 200);
            }
        });

答案 1 :(得分:-1)

我认为您可以在this page回答中找到答案,但作为一个普通法,我会复制解决方案,以便我的回答不会导致链接。

<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
    android:focusable="true" 
    android:focusableInTouchMode="true"
    android:layout_width="0px" 
    android:layout_height="0px"/>

<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:nextFocusUp="@id/autotext" 
    android:nextFocusLeft="@id/autotext"/>

如果不能解决,请告诉我。

相关问题