EditText会自动转到下一行,输入字词为whatsapp editText

时间:2018-05-06 21:15:02

标签: java android android-studio android-edittext android-textinputedittext

EditText

<android.support.v7.widget.AppCompatEditText
            android:id="@+id/itemEditText_id"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_marginTop="8dp"
            android:layout_marginStart="2dp"
            android:layout_marginEnd="4dp"
            android:lines="2"
            android:scrollHorizontally="false"
            android:maxLength="69"
            android:scrollbars="vertical"
            android:background="@drawable/round_border_edit_text"
            android:hint="Go ahead \nSend messge"
            android:inputType="textMultiLine|textLongMessage"
            android:textAppearance="@style/TextAppearance.AppCompat.Body2"
            android:maxLines="2"
            android:minLines="1"
            />

这是我的xml代码,如果需要添加或删除某些内容请告知。

我正在使用的java代码来实现我想要的whatsapp的editText,但它不能很好地工作

final AppCompatEditText editText = holder.itemEditText;

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {



                // if edittext has 10chars & this is not called yet, add new line
                if(editText.getText().length() == 34 && !isReached) {

                    editText.append("\n");
                    isReached = true;

                }
                // if edittext has less than 10chars & boolean has changed, reset
                if(editText.getText().length() < 34 && isReached) isReached = false;

                Log.d("char", "onTextChanged: "+charSequence);
                Log.d("i", "onTextChanged: "+i);
                Log.d("i1", "onTextChanged: "+i1);
                Log.d("i2", "onTextChanged: "+i2);

            }

            @Override
            public void afterTextChanged(Editable editable) {


            }
        });

    }

此代码无法正常工作,或者您可以说我无法按照我希望editText行事的方式进行编码。

我希望我的editText像Whatsapp的editText一样工作,就像当我输入内容并且它到达editText内的图标时,光标会转到带有输入字的新行。在我的editText中,单词位于ImageButton下面,我将其用作发送图标。我不希望这些文字出现在发送图标下面。请帮忙。

的ImageButton

 <android.support.v7.widget.AppCompatImageButton
            android:id="@+id/itemSendButton_id"
            android:layout_width="25dp"
            android:layout_height="20dp"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="11dp"
            android:background="@color/colorFacebookBtnText"
            android:clickable="true"
            android:focusable="true"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_send" />

我是编程新手所以请提前帮助我并提前致谢。

1 个答案:

答案 0 :(得分:0)

使用此

et1.addTextChangedListener(new TextWatcher() {

public void onTextChanged(CharSequence s, int start,int before, int count) 
{
    // TODO Auto-generated method stub
    if(et1.getText().toString().length()==size)     //size as per your requirement
    {
        et2.requestFocus();
    }
}
public void beforeTextChanged(CharSequence s, int start,
                int count, int after) {
            // TODO Auto-generated method stub

}

public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
}

});