简体   繁体   English

当edittext android中的当前行为空时,如何防止用户换行?

[英]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 XML 文件

<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 myEditText.java

public class myEditText extends androidx.appcompat.widget.AppCompatEditText {公共类 myEditText 扩展 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.您可以在 xml 中使用数字,但所做的只是应用一个 textwatcher,它消除了不符合预期的更改,并且它不是有条件的。

You can get what you want fairly easily though.你可以很容易地得到你想要的东西。 Create a new TextWatcher with the following function:使用以下函数创建一个新的 TextWatcher:

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.如果不是,它将退出而不更改文本。

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

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