简体   繁体   中英

How to prevent user for going new line when the current line is empty in edittext android?

I have this code for printing bullet and number in my edit text

XML File

<com.example.nutrifood.myEditText
            android:id="@+id/mealIngredientsInput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:cursorVisible="true"
            android:fontFamily="@font/hindmadurai_regular"
            android:gravity="top"
            android:inputType="textMultiLine"
            android:minLines="4"
            android:paddingStart="25dp"
            android:paddingEnd="20dp"
            android:textColor="@color/light"
            android:textSize="16sp" />

myEditText.java

public class myEditText extends androidx.appcompat.widget.AppCompatEditText {

public Rect rect = new Rect();
public Paint paint = new Paint();
private myEditText editTextinput = findViewById(R.id.editTextinput);
boolean isTyping;

public myEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    
   ...

    editTextInput.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
            if (text.length() == 0) {
                isTyping = false;
                return;
            }            
            isTyping = true;
        }

     ...
    });
}

@Override
protected void onDraw(Canvas canvas) {
    if (isTyping) {
        int baseline = getBaseline();
        for (int i = 0; i < getLineCount(); i++) {
            canvas.drawText("  •", rect.left, baseline, paint);
            baseline += getLineHeight();
        }
    }
    super.onDraw(canvas);
}

}

This will give me the output like this.

• One

• Two

Is there a way to disable the new line in keyboard when the current line is empty?

There is no way to disable keys in the keyboard, especially not conditionally. You can use digits in the xml, but all that does is apply a textwatcher which eliminates changes that aren't what it expects, and its not conditional.

You can get what you want fairly easily though. Create a new TextWatcher with the following function:

void onTextChanged (CharSequence s, 
                int start, 
                int before, 
                int count) {
    String newText = s.toString().replace("\n\n","\n");
    if(newText.charAt(0) == '\n') {
        newText = newText.substring(1);
    }
    if(!s.toString().equals(newText)) {
       setText(newText);
    }
}

Basically, that looks for any double newlines an replaces it with single newlines. Then it chops off any leading newlines. If either of those things changes the string (if any of them were found) it sets the text to that new string formed by fixing them. If not, it exits without changing the text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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