【问题标题】:Dynamically auto-capitalize, auto-correct, or password field in an EditTextEditText 中的动态自动大写、自动更正或密码字段
【发布时间】:2012-04-04 04:54:06
【问题描述】:

我想以编程方式启用或禁用 EditText 中的自动大写、自动更正或密码字段(显示项目符号)。 这意味着不是来自 XML

我也想避免TextWatcher 解决方案,而更多地关注InputFilter 或其他一些解决方案。

将 EditText 操作为 Editable 允许附加 InputFilters,但是我无法让这些以编程方式工作。此外,setAllCaps 等 EditText 方法在实践中对我没有任何作用。自动校正也是如此。这是我尝试的自动更正(向您展示我在哪里,以及我的一些思考过程):

/** SpellCheck filter for auto-correcting words. */
class SpellCheckFilter implements InputFilter {

    public String word;

    public SpellCheckFilter()
    {
        word = " ";
    }

    //FIXME not returning corrected word. Try adjusting start/end values,
    //what range does this return?
    @Override
    public CharSequence filter(CharSequence source, int start, int end,
            Spanned dest, int dstart, int dend) {
        word += source;
        Log.i("SpellCheckFilter", "source=\"" + source + "\";  word=\"" + word + "\"");
        if (source.toString().endsWith(" "))
        {
            word = word.replace(" ", "");
            String correction = AutoText.get(word, 0, word.length()-1, view);
            Log.i("TextEditor", "Corrected word=" + (correction == null ? word : correction));
            word = " ";
            return correction;
        }
        return null;
    }

}

使用InputFilter.AllCaps,我能够得到一个几乎可以工作的自动大写方法,但是第一个字母没有自动大写。

【问题讨论】:

  • 投反对票的人能否解释一下原因???
  • 不是我的反对意见。但是,如果您详细说明您的尝试(包括代码),您会改进您的问题。

标签: android android-edittext input-filtering


【解决方案1】:
    //Calling Method
    setListener1(editText);
    //Method
    public void setListener1(final AppCompatEditText edittext) {
            final TextWatcher textWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {}
                @Override
                public void afterTextChanged(Editable s) {
                    String input = StringStaticMethods.firstCapital(s.toString());
                    edittext.removeTextChangedListener(this);
                    edittext.setText("");
                    edittext.append(input);
                    edittext.addTextChangedListener(this);
                }
            };
            edittext.addTextChangedListener(textWatcher);
        }
public static String firstCapital(String str) {
        if (str != null && !str.equals("")) {
            return (str.substring(0, 1).toUpperCase() + str.substring(1, str.length()));
        }
        return "";
    }
    //Use in XML
    android:inputType="textFilter|textMultiLine|textNoSuggestions"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 2012-05-07
    • 2011-04-17
    • 2013-11-02
    • 2019-11-30
    相关资源
    最近更新 更多