InputFilter未应用于EditText

时间:2013-11-26 14:41:51

标签: android android-layout android-fragments dialog

在下面的代码中,我给包含2个EditTexts的简单布局进行了膨胀,并尝试对它们应用过滤器,以便只允许大写(或实际上强制降低到大写)字母和数字。不允许使用空格或其他字符,并且长度是固定的。 EditText似乎没有采用这个过滤器,因为大写惯例和长度都被忽略,我可以自由输入任何我想要的东西。我之前使用过类似的代码,所以我认为它与我如何膨胀它有关,或者它在Dialog中。

    private void createCustomNotice() {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = getLayoutInflater();
    final View v = inflater.inflate(R.layout.dialog_new_report, null);
    builder.setView(v)
        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                EditText etFoo = (EditText) v.findViewById(R.id.etxtFoo);
                InputFilter filter = new CustomTextFilter();
                etFoo.setFilters(new InputFilter[]{filter, new InputFilter.LengthFilter(10)});


                EditText etBar = (EditText) v.findViewById(R.id.etxtBar);
                InputFilter barFilter = new FooTextFilter();
                etBar.setFilters(new InputFilter[]{barFilter, new InputFilter.LengthFilter(2)});


                String Foo = etFoo.getText().toString();
                String troubleCode = etBar.getText().toString();
                reportIntent(Foo.toUpperCase(), troubleCode.toUpperCase()); //Starts an intent
            }
        })
        .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
    builder.create().show();
}



class CustomTextFilter implements InputFilter {

    @Override
    public CharSequence filter(CharSequence source, int start, int end,
            Spanned dest, int dstart, int dend) {

        if (source instanceof SpannableStringBuilder) {
            SpannableStringBuilder sourceAsSpannableBuilder = (SpannableStringBuilder) source;
            for (int i = end - 1; i >= start; i--) {
                char currentChar = source.charAt(i);
                if (!Character.isLetterOrDigit(currentChar)
                        && !Character.isSpaceChar(currentChar)) {
                    sourceAsSpannableBuilder.delete(i, i + 1);
                }
            }
            return source.toString().toUpperCase();
        } else {
            StringBuilder filteredStringBuilder = new StringBuilder();
            for (int i = start; i < end; i++) {
                char currentChar = source.charAt(i);
                if (Character.isLetterOrDigit(currentChar)
                        || Character.isSpaceChar(currentChar)) {
                    filteredStringBuilder.append(currentChar);
                }
            }
            return filteredStringBuilder.toString().toUpperCase();
        }
    }

}   

我的虚增布局是这样的:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/etxtFoo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textVisiblePassword"
        android:hint="@string/Foo_with_example"
        android:ems="10" >

        <requestFocus />
    </EditText>
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/etxtBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textVisiblePassword"
        android:hint="@string/Bar_with_example"
        android:ems="10" >

        <requestFocus />
    </EditText>
</LinearLayout>

任何人都可以建议我可能做错了导致输入过滤器被忽略吗?

1 个答案:

答案 0 :(得分:1)

您确定要在onClick方法中设置过滤器吗?这看起来很奇怪。 我会说移动这段代码:

EditText etFoo = (EditText) v.findViewById(R.id.etxtFoo);
InputFilter filter = new CustomTextFilter();
etFoo.setFilters(new InputFilter[]{filter, new InputFilter.LengthFilter(10)});


EditText etBar = (EditText) v.findViewById(R.id.etxtBar);
InputFilter barFilter = new FooTextFilter();
etBar.setFilters(new InputFilter[]{barFilter, new InputFilter.LengthFilter(2)});

就在这条线下面:

final View v = inflater.inflate(R.layout.dialog_new_report, null);