【问题标题】:How to prevent deleting the first character of edit text in Android如何防止在Android中删除编辑文本的第一个字符
【发布时间】:2015-07-10 01:41:12
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

  • 你到现在都试过什么???
  • 我是编程新手。我的逻辑很薄弱。但不能写代码..当textchange检查它类似于'/'如果不允许删除..如果它类似于'/'不允许删除......但我不能为此编写代码:(
  • 你为什么要这样做??我猜你只想要那个edittext中的文本。因此,您可以简单地获取文本并将文本附加“/”作为第一个字符。你得到你想要的。带有第一个字符“/”的文本。
  • 这可能会有所帮助:[在 EditText 中放置一个不可编辑的常量文本][1] [1]:stackoverflow.com/questions/14195207/…

标签: android android-edittext substring textview


【解决方案1】:

EditText 无法实现这样的行为

但你可以做一些变通方法

在 Android 中使用 TextWatcher。这样就可以监听EditText中的变化了

Here is a complete Example code snippet

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 有史以来最好的解决方案,有时我们会过度思考解决方案而忘记了简单性:)
    【解决方案3】:

    有几种方法可以实现这一目标。首先,您可以简单地拥有它,这样如果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 相结合的解决方案。但是,由于您的问题很具体,我已尝试直接回答。

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • 但用户应该看到'/'
      • 如果用户看不到 '/' 它是什么类型的输入,为什么这很重要
      【解决方案5】:

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

      我的问题是,为了防止用户删除我的 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 个字符,并且在文本的开头总是有一个前缀。

      快乐的编码!如果你觉得这很有用,那就干杯。

      【讨论】:

      • 你从哪里得到 count 的值来自if (count &lt; 3)
      • int count = (s == null) ? 0 : s.toString().length(); ?
      【解决方案6】:

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

      【讨论】:

        【解决方案7】:

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

        【讨论】:

        • 优秀的解决方案。
        • 谢谢,您的回答对我有帮助!
        【解决方案8】:

        #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) {
                    }
                })
        

        这样用户将无法移除/删除定义的前缀。而且,第一个输入的字符也会被追加到编辑框中。

        #KOTLIN_EXAMPLE

        #HappyCoding

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-10
          • 1970-01-01
          • 2014-04-16
          • 2010-10-23
          • 1970-01-01
          • 2018-11-09
          相关资源
          最近更新 更多