0
아래 코드에서 2 개의 EditText를 포함하는 간단한 레이아웃을 확장하고 필터를 적용하여 대문자 만 허용하거나 실제로는 대문자 만 허용합니다. 공백이나 다른 문자는 허용되지 않으며 길이는 고정되어 있습니다. EditText는이 대문자 규칙과 길이가 모두 무시되고 원하는대로 입력 할 수 있으므로이 필터를 사용하지 않는 것처럼 보입니다. 이전에 비슷한 코드를 사용했기 때문에 내가 어떻게 팽창하는지 또는 대화 상자에 있는지와 관련이 있다고 생각합니다. InputFilter가 EditText에 적용되지 않습니다.
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>
사람이 내가 입력 필터를 무시하는 원인이 잘못된 일을 할 수 있습니다 무엇을 제안 할 수 있습니다?
당신은 그것을 못을 박았다. 총 두뇌 방귀! – xceph
도와 주셔서 감사합니다 :) – Mark