编辑类型时间的解决方法,显示没有冒号的键盘

时间:2012-09-15 20:58:35

标签: android android-softkeyboard

我需要让用户以[{1}}格式输入时间,但由于issue 28132我无法使用

hh:mm

我以为我会接受任何分隔符,例如<EditText ... android:inputType="time"> hh mm之类的东西,但这些字符也不能输入(逻辑因为它们不属于时间;冒号确实但是键盘上缺少了)。将类型更改为hh.mm会起作用,但文本键盘不适合键入。

所以我考虑在编辑开始之前删除冒号并在结束时将其放回,但我不知道如何识别这些事件。 text允许跟踪所有细粒度的更改,但我认为在编辑时更改文本是不合理的,我宁愿需要像{{1}这样的事件}和addTextChangedListener,对应于显示和隐藏键盘。 他们是这样的事件吗?

您会推荐针对此错误的解决方法吗?

1 个答案:

答案 0 :(得分:1)

您应该使用InputFilter作为TextView并验证输入是否为Time类型。

由于TimeKeyListener也实现了一个InputFilter。

你可以使用

 TextView.setInputFilter({new TimeKeyListener()});

编辑: 或者您甚至可以使用作为可接受的字符

来自定义时间键监听器
    edittext.setKeyListener(new TimeKeyListener() {
        public final char[] CHARS = new char[] {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'm', 'p', ':', '.'
        };

        @Override
        protected char[] getAcceptedChars() {
            return CHARS;
        }
    });

现在用冒号替换替代分隔符(点)也很容易,如下所示:

public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
    final CharSequence superSource = super.filter(source, start, end, dest, dstart, dend);
    final CharSequence prefilteredSource = superSource!=null ? superSource : source;
    return prefilteredSource.toString().replace('.', ':');
}