Android编辑文本某些文本始终不可编辑
我想在Android编辑文本某些文本始终不可编辑,android,android-edittext,Android,Android Edittext,我想在EditText上放置一些自定义文本。这将是不可编辑的,并且总是出现在编辑文本上。当我们在编辑文本上添加内容时,我的不可编辑文本应该向前移动 默认情况下应该出现类似的内容 不可编辑文本 当我在编辑文本上键入内容时,它应该显示如下 Hello World不可编辑文本 也许您可以使用addTextChangedListener和您的EditText来完成这项工作 但要小心: This method is called to notify you that, somewhere within s
EditText
上放置一些自定义文本。这将是不可编辑的,并且总是出现在编辑文本上。当我们在编辑文本上添加内容时,我的不可编辑文本应该向前移动
默认情况下应该出现类似的内容
不可编辑文本
当我在编辑文本上键入内容时,它应该显示如下
Hello World不可编辑文本
也许您可以使用
addTextChangedListener
和您的EditText
来完成这项工作但要小心:
This 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()
用户输入,并在EditText
的末尾添加字符串
希望这有帮助
EditText的文本是“(提示)”,这肯定能起作用。您只需在每个事件中设置输入文本+(不可编辑文本)。谢谢您的帮助!这对我有用。我过去经常换衣服。它就像我想要的那样工作。有一种程序化的方法。一旦我弄明白了,我会马上写一个答案。但这里有另一种方法:请你看看我的答案,看看它是否适合你。留下评论和一些反馈。
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();
}
});
}