简体   繁体   English

EditText使用诸如whatsapp editText之类的输入词自动转到下一行

[英]EditText automatically go to next line with the typing word like whatsapp editText

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"
            />

this is my xml code if something needs to be added or removed here please tell. 如果需要在此处添加或删除某些内容,这是我的xml代码。

java code which i'm using to achieve what i want like whatsapp's editText but its not working quite well 我正在使用的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) {


            }
        });

    }

this code is not working like i want it to or you can say i'm not being able to code the way i want editText to act. 这段代码无法像我想要的那样工作,或者您可以说我无法以我希望editText起作用的方式进行编码。

i want my editText to work like Whatsapp's editText like when i type something and it reaches to the icon inside the editText, cursor goes to a new line with the typing word. 我希望我的editText像Whatsapp的editText一样工作,例如当我键入某些内容并到达editText内的图标时,光标会与键入的单词移到新行。 In my editText the words are going underneath the ImageButton which im using as a send icon. 在我的editText中,单词在ImageButton的下面,即时通讯用作发送图标。 i dont want the words to go underneath the send icon. 我不希望这些单词出现在“发送”图标下方。 Please Help. 请帮忙。

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" />

i'm new to programming so please help me out and thanks in advance. 我是编程新手,所以请帮助我,并提前致谢。

Use this 用这个

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
}

});

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

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