如何防止删除 Android 中编辑文本的第一个字符

How to prevent deleting the first character of edit text in Android

这是我在应用程序中编辑的文本之一

<EditText
        android:id="@+id/etFolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="/pictures"
        android:ems="10" >

第一次出现时带有“/pictures”

用户可以更改文本并输入另一个词。但是如何防止删除编辑文本的“/”。

用户可以删除所有其他文本,但不允许删除第一个字符。

我怎样才能实现这种行为?

您无法为 EditText 实现这样的行为

但是你可以做一些变通方法

在 Android 中使用 TextWatcher。所以你可以在EditText

中监听变化

Here is a complete Example code snippet

您可以将您的 EditText 放在您拥有的主布局中的单独 RelativeLayout 中,并在同一行上添加一个带有文本 / 的 TextView,然后仅在 EditText 文本中保留 "pictures" 部分。像这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text = "/"/>
    <EditText
        android:id="@+id/etFolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="pictures"
        android:ems="10"
        android:layout_marginLeft="10dp" >
</RelativeLayout>

您可以通过多种方式实现这一目标。首先,您可以简单地使用它,这样如果 EditText 块为空,它会立即用“/”字符重新填充。或者,如果前面的 char/,则防止用户删除回来。以下代码未经测试,因此您可能需要稍微调整一下。

editText = (EditText) findViewById(R.id.editText);
    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (editText.getText().charAt(editText.length()-1) == '/') {
                editText.append(" ");
            }

            //OR...

            if (editText.length() == 0) {
                editText.setText("/")
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

编辑: FWIW,如果我处在您的位置,我个人会选择 Gabriella 和 apk 相结合的解决方案。但是,由于你的问题很具体,我尽量直接回答。

与其这样做,不如让用户输入他想要的任何文本,并且当您调用 getText() 时在开头附加“/”,如下所示

String text = "/"+editText.getText();

这里可能有一个可接受的答案,但对我没有任何帮助,所以对于那些寻找替代方法的人来说,这里是代码。

我的问题是,为了防止用户删除我 EditText 中的前 3 个字符,这是 phone 号码 (+63)

的区号
public class TextListener implements TextWatcher {

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (count < 3) {
           etMobileNumber.setText("+63");
           etMobileNumber.setSelection(count + 1);
        }
    }
}

//
// use it like this
//
etMobileNumber.addTextChangedListener(new TextListener());

这样用户将无法 delete/remove 我的 EditText 上的前 3 个字符,并且文本的开头将始终有一个前缀。

编码愉快!如果你觉得这有用,干杯。

的基础上,解决了快速删除时插入符号在前缀中间结束的问题:

editText.addTextChangedListener(new TextWatcher() {

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable editable) {

        String prefix = "+63";

        int count = (editable == null) ? 0 : editable.toString().length();
        if (count < prefix.length()) {

            editText.setText(prefix);

            /*
             * This line ensure when you do a rapid delete (by holding down the
             * backspace button), the caret does not end up in the middle of the
             * prefix.
             */
            int selectionIndex = Math.max(count + 1, prefix.length());

            editText.setSelection(selectionIndex);
        }
    }
});

Kotlin 示例。你可以这样做:

editText.addTextChangedListener(object: TextWatcher {
        override fun afterTextChanged(s: Editable?) {

        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            if (editText.length() < 5 || !editText.text.startsWith("+998 ")) {
                editText.setText("+998 ")
                editText.setSelection(editText.length());
            }
        }
    })

#KOTLIN_EXAMPLE

    editText.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                val prefix = "+91 "
                val count = s?.toString()?.length ?: 0
                if (count < prefix.length) {
                    if (s.toString() != prefix.trim()) {
                        editText.setText("$prefix$s")
                    } else {
                        editText.setText(prefix)
                    }
                    editText.setSelection(editText.length())
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })

这样用户将无法remove/delete定义的前缀。此外,第一个输入的字符也会被追加到编辑框中。

#KOTLIN_EXAMPLE

#HappyCoding