Android Edit Text Some Text Always there 不可编辑

标签 android android-edittext

我想在 EditText 上放置一些自定义文本。这将是不可编辑的,并且总是出现在编辑文本上。当我们在编辑文本上添加一些内容时,我的不可编辑文本应该向前移动。

默认情况下应该出现这样的内容

Non editable Text

当我在编辑文本中输入内容时,它应该是这样的

Hello World Non editable Text

最佳答案

The text of the EditText is "(hint)" <-- this is important because of the lengths I use

这是代码。然而,唯一的问题似乎是当用户退格时,它不会将光标放在正确的位置。您或许可以使用 setSelection(int) 行来解决这个问题。如果我弄明白了,我会进行更新。

注意:所有Toasts只是测试,所以您知道代码发生了什么。

String pastText = ""; // represents the text BEFORE the text is changed each time
EditText et;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    et = (EditText)findViewById(R.id.et);

    // run this code every time the EditText String is changed
    et.addTextChangedListener(new TextWatcher()
    {
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            // length of the String in the EdiText
            int length = et.getText().toString().length(); 

            // is the String of the EditText from first position to length - 6 position
            // (take away the non-editable text)
            String editableText = et.getText().toString().substring(0, length - 6);

            int cursor = et.getSelectionStart(); // where the cursor currently is

            // minSelection is the second position number of the non-editable
            // text in the EditText. It's the second position because the 
            // cursor needs to be BEFORE it, and the comparison will be 
            // if the cursor is LESS than minSelection.
            // so it's the length, subtract 5, because the non-editable text
            // is 6 chars long. Ex: if the EditText was "test(hint)" then
            // minSelection would be the position (as int) of 'h' in the String
            int minSelection = length-5;

            // if the user has his cursor BEFORE the non-editable text
            if (cursor < minSelection)
                Toast.makeText(getApplicationContext(), "good to type", 0).show();
            else { // otherwise (he can't edit)
                Toast.makeText(getApplicationContext(), "can't type", 0).show();
                int currentCursor = et.getSelectionStart(); // get where the cursor is
                et.setText(pastText); // set the text to what it used to be
                et.setSelection(currentCursor-1); // set cursor where it previously was
            }

            Toast.makeText(getApplicationContext(), "past text: " + pastText, 0).show();

            Toast.makeText(getApplicationContext(), 
                           "text: " + editableText + '\n' + '\n' 
                           + "cursor: " + cursor + '\n' + '\n' 
                           + "minSelection: " + minSelection, 0).show();
        }

        @Override public void afterTextChanged(Editable s) {} 

        @Override 
        public void beforeTextChanged(CharSequence s, int start, int count, int after) 
        { 
            // in the method beforeTextChanged, set pastText to be the text
            // of the EditText before the text is changed
            pastText = et.getText().toString(); 
        }
    });
}

关于Android Edit Text Some Text Always there 不可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20084892/

相关文章:

java - EditText getText 返回空

android - 没有 SyncAdapter 的 AccountManager?

java - Android-选项卡 Activity 出现错误

Android 位置管理器每次都返回相同的 gps 坐标

android - Toast 消息要求用户填写空白 "EditText"字段

android - TextView.setVisibility(GONE) 不工作

android - 按下后退按钮时访问 EditText 的内容

Android EditText 不起作用,android :imeOptions ="actionNext" android:inputType ="phone"

java - java [android] 中小数点后 3 位的 EditText 掩码

Android 评分栏 - 如何预设星级