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

时间:2015-04-30 11:16:00

标签: android android-edittext substring textview

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

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

第一次出现时会出现“/ pictures”

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

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

我该如何实现这种行为?

7 个答案:

答案 0 :(得分:4)

您可以将EditText放在您拥有的主布局中的单独RelativeLayout中,并在同一行上添加一个带有text /的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>

答案 1 :(得分:4)

有几种方法可以实现这一目标。首先,您可以简单地使用它,以便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这样的解决方案。但是,由于您的问题具体,我试图直接回答。

答案 2 :(得分:1)

您无法为EditText

实现此类行为

但你可以做一些解决方法

在Android中使用TextWatcher。因此,您可以收听EditText中的更改

Here is a complete Example code snippet

答案 3 :(得分:1)

而不是这样做让用户输入他想要的任何文本,当你调用getText()时,附加&#34; /&#34;在开始时如下

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

答案 4 :(得分:1)

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());
            }
        }
    })

答案 5 :(得分:0)

这里可能有一个可以接受的答案,但对我没有任何帮助,因此对于那些寻求其他方法的人来说,这里的代码。

我的问题是,阻止用户删除我的EditText中的前3个字符,该字符用于电话号码(+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());

通过这种方式,用户将无法删除/删除我的EditText上的前3个字符,并且始终在文本开头添加前缀。

快乐的编码!如果您觉得这很有用,那就很高兴。

答案 6 :(得分:0)

基于ralphgabb's answer,当您进行快速删除时,这克服了插入符号最终出现在前缀中间的问题:

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);
        }
    }
});