Android EditText中的永久提示

Android EditText中的永久提示,android,android-edittext,Android,Android Edittext,我希望用户为我完成一个句子。例如,考虑一个“代码>编辑文本< /代码>,并带有一个“最后一次”的提示。通常,当用户单击编辑文本时,提示会消失,但我希望它保持不变。此外,我希望文本是永久的,这样它就不能被删除。。。让用户只有一个选项。。。完成句子 第一部分相当简单,只需使用EditText的setText()方法来放置提示。困难的部分是后者。我怎么能在EditText中有用户无法擦除的文本呢?那么,你不能在代码中这样做吗?一些算法如,如果文本少于16个字符(长度为“上次我”)则将文本设置为该长度。

我希望用户为我完成一个句子。例如,考虑一个“代码>编辑文本< /代码>,并带有一个“最后一次”的提示。通常,当用户单击编辑文本时,提示会消失,但我希望它保持不变。此外,我希望文本是永久的,这样它就不能被删除。。。让用户只有一个选项。。。完成句子


第一部分相当简单,只需使用EditText的
setText()
方法来放置提示。困难的部分是后者。我怎么能在
EditText
中有用户无法擦除的文本呢?

那么,你不能在代码中这样做吗?一些算法如,如果文本少于16个字符(长度为“上次我”)则将文本设置为该长度。因此,每当他们点击它时,如果他们试图删除它,它就会返回默认文本


还有另一个想法,为什么不让文本视图的右边缘与编辑文本框的左边缘对齐,用户永远不会知道它是另一个框。这确实是最好的解决方案,如果您不想编辑文本,只需将其设置为文本视图即可,仅检查长度是不够的。。。我可以输入“这是一个很长的文本,我放进了框中”,它会接受它,即使它不是以“上次我”字符串开头


就个人而言,我可能会选择建议的预防方法,即使用文本视图,而不是在退出时使用检查。但是如果要在以后验证它,实际上需要检查返回字符串的开头

所描述的问题可以使用android.text.TextWatcher解决

public class CompleteSentenceWathcher implements TextWatcher {

    private final String initialText;

    private int start;
    private int after;
    private int count;

    public CompleteSentenceWathcher(String initialText) {
        this.initialText = initialText;
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        this.start = start;
        this.count = count;
        this.after = after;
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        if(start < initialText.length()) {
            if(s.toString().startsWith(initialText)) {
                return;
            }
            if(count >= 1 && after == 0) {
                if(start+count+1 <= initialText.length()) {
                    s.replace(start, start+count, initialText.substring(start, start+count+1));
                } else {
                    s.replace(start, start, initialText.substring(start, start+1));
                }
            } else if(count == 0 && after >= 1) {
                s.delete(start, start+after);
            }
        }
    }

}

我已经用一个
InputFilter
实现了这一点,其中
\u PERMANENT\u HINT\u TEXT
EditText
末尾的文本,我不希望用户能够修改它。我建议给它添加一个颜色范围,这样它就会变灰,希望看起来像文本的提示/禁用部分。这将有望改善用户体验,因为他们应该自动假定它是不可修改的,而不仅仅是想知道为什么
EditText
(他们通常可以完全更改)的某些部分不“工作”。这种方法允许在 在
EditText
上设置了
InputFilter
,这是我的一项要求,因为我在
EditTextPreference
上使用了它

为了清楚起见,我需要永久文本存在于
EditText
的末尾,而不是开头,但这应该与我的实现对称

new InputFilter() {
    @Override
    public CharSequence filter(CharSequence source, int source_start, int source_end,
                               Spanned destination, int destination_start, int destination_end) {
        final int protected_text_start = (TextUtils.isEmpty(destination)? source.length() : destination.length()) - _PERMANENT_HINT_TEXT.length();
        // Allows input into unprotected region
        if (source_start + destination_start - source_end < protected_text_start)
            return null;
        // Prevents deletion of protected region
        else if (TextUtils.isEmpty(source))
            return destination.subSequence(destination_start, destination_end);
        // Ignores insertion into protected region
        else
            return "";
    }
}
newInputFilter(){
@凌驾
公共CharSequence筛选器(CharSequence source、int source\u start、int source\u end、,
跨距目的地、整数目的地(起点、整数目的地(终点)){
final int protected_text_start=(TextUtils.isEmpty(destination)?source.length():destination.length())-_PERMANENT_HINT_text.length();
//允许输入到不受保护的区域
if(源\u开始+目标\u开始-源\u结束<受保护的\u文本\u开始)
返回null;
//防止删除受保护区域
else if(TextUtils.isEmpty(源))
返回目的地。子序列(目的地\开始,目的地\结束);
//忽略插入到受保护区域
其他的
返回“”;
}
}

使用
EditText.setFilters(新的InputFilters[]{/*InputFilter放在这里*/};
将其添加到所需的
EditText

我喜欢第一个想法。我现在正在尝试。您为我指明了正确的方向。我将实现
TextWatcher
接口,然后在
posterextchanged
方法中,查看前X个字符是否是我想要的……如果不是,请更正它们。好主意,就是这样想知道,你为什么不把“上次我”放在文本视图中?嗯…什么?不,不会的。它会检查你的字符串是否以“上次我”开头。如果有,则什么也没有。如果没有,则会修复它。由于每次textchange都会调用此方法,因此不会像您的示例中那样失控。文本在指定区域中只能短一个字符或长一个字符。我的检查类似于:
If inputStr.startsWith(startString)
它不检查长度。这是正确的…startsWith()完全按照我的建议执行-“检查返回字符串的开头”。它不执行第一个答案建议的“某种算法,如果文本少于16个字符(长度为“上次I”),则将文本设置为该长度。”
new InputFilter() {
    @Override
    public CharSequence filter(CharSequence source, int source_start, int source_end,
                               Spanned destination, int destination_start, int destination_end) {
        final int protected_text_start = (TextUtils.isEmpty(destination)? source.length() : destination.length()) - _PERMANENT_HINT_TEXT.length();
        // Allows input into unprotected region
        if (source_start + destination_start - source_end < protected_text_start)
            return null;
        // Prevents deletion of protected region
        else if (TextUtils.isEmpty(source))
            return destination.subSequence(destination_start, destination_end);
        // Ignores insertion into protected region
        else
            return "";
    }
}