开发者

Permanent hint in EditText

开发者 https://www.devze.com 2023-03-22 23:29 出处:网络
I have a situation where I would like the user to complete a sentence for me. For example, consider a EditText with a hint of \"The last time I \". Normally, when a user clicks an EditText, the hint 开

I have a situation where I would like the user to complete a sentence for me. For example, consider a EditText with a hint of "The last time I ". Normally, when a user clicks an EditText, the hint 开发者_StackOverflow中文版disappears, but I would like it to stay. Additionally, I would like the text to be permanent, so that it cannot be erased... leaving the user with only one option... complete the sentence.

The first part is fairly simple, just use the setText() method of EditText to place the hint. The difficult part is the latter. How can I have text in an EditText that the user cannot erase?


Well couldn't you do it in code? Some algorithim like, if the text is less than 16 characters (length of "The last time I ") then set the text to that. Therefore whenever they clicked it, if they tried to erase it, it would just go back to the default text.

Also, another idea..why don't you just make a TextView thats right edge aligns with the left edge of the EditText box, the user would never know that it was another box. This is acutally the best solution, if you don't want the text ever to be edited, just make it a TextView


Described problem can be solved using 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);
            }
        }
    }

}

Create an instance of EditText and add the TextWatcher.

EditText editText = new EditText(this);
        editText.setText("I love");
        editText.addTextChangedListener(new CompleteSentenceWathcher(editText.getText().toString()));


I've implemented this with an InputFilter, where _PERMANENT_HINT_TEXT is the text at the end of the EditText that I don't want the user to be able to modify. I recommend adding a color span to it, so that it is grayed out to hopefully look like a hint/disabled section of text. This should hopefully improve the UX as they should automatically assume it is unmodifiable, and not just wonder why some part of the EditText (that they usually can completely change) isn't "working". This approach allowed the text to be set after the InputFilter was set on the EditText, which was a requirement for me since I used this on an EditTextPreference.

To be clear, I needed the permanent text to exist at the end of the EditText, instead of the beginning, but that should be symmetrical to my implementation.

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 "";
    }
}

use EditText.setFilters(new InputFilters[] { /* InputFilter goes here */ }; to add it to the desired EditText.


Just checking for the length wouldn't be adequate... I could type "This is a really long text I put into the box" and it would accept it even though it doesn't begin with "The last time I" string.

Personally, I would probably go for the prevention method suggested of using a TextView over that of a check on the way out. But if you're going to validate it afterwards, you'd actually need to check the beginning of the returned string.

0

精彩评论

暂无评论...
验证码 换一张
取 消