Android编辑文本一些文字始终不可编辑

时间:2013-11-20 00:09:45

标签: android android-edittext

我想在EditText上添加一些自定义文字。哪些是不可编辑的并且总是出现在编辑文本上。当我们在编辑文本上添加一些东西时,我的不可编辑文本应该继续前进。

默认会出现类似这样的内容

  

不可编辑的文字

当我在编辑文本上输入内容时,它应该显示为

  

Hello World 不可编辑的文字

2 个答案:

答案 0 :(得分:1)

也许您可以使用addTextChangedListener EditTextThis method is called to notify you that, somewhere within s, the text has been changed. It is legitimate to make further changes to s from this callback, but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively. (You are not told where the change took place because other afterTextChanged() methods may already have made other changes and invalidated the offsets 进行此操作 但要小心:

getLength()

(正如您在此处所见:https://stackoverflow.com/a/10862398/2668136

您需要阅读以下答案:

  1. Put constant text inside EditText which should be non-editable - Android
  2. Android java : Update same EditText in textChanged event
  3. Access edittext from textwatcher
  4. 我想您可以String用户的输入,并在EditText的末尾添加{{1}}。
    希望这有用。

答案 1 :(得分:1)

  

EditText的文本是“(提示)”< - 这很重要,因为我使用的长度

这是代码。但是,唯一的问题似乎是当用户退格时,它不会将光标放在正确的位置。您可以使用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(); 
        }
    });
}