下面列出了android.widget.EditText#setFilters ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Ensures that the given `EditText` component will have a maximum length as specified
*
* @param editText the `EditText` component
* @param maxLength the maximum length to guarantee
* @param existingInputFilters a list of existing input filters to keep
*/
public static void setMaxLength(final EditText editText, final int maxLength, final InputFilter[] existingInputFilters) {
final InputFilter[] allInputFilters;
if (existingInputFilters == null) {
allInputFilters = new InputFilter[1];
allInputFilters[0] = new InputFilter.LengthFilter(maxLength);
}
else {
allInputFilters = new InputFilter[existingInputFilters.length+1];
for (int i = 0; i < allInputFilters.length; i++) {
if (i < existingInputFilters.length) {
allInputFilters[i] = existingInputFilters[i];
}
else {
allInputFilters[i] = new InputFilter.LengthFilter(maxLength);
}
}
}
editText.setFilters(allInputFilters);
}
/**
* Init number picker by disabling focusability of edit text embedded inside the number picker
* We also override the edit text filter private attribute by using reflection as the formatter is still buggy while attempting to display the default value
* This is still an open Google @see <a href="https://code.google.com/p/android/issues/detail?id=35482#c9">issue</a> from 2012
*/
private void initView() {
setMinValue(MIN_VALUE);
setMaxValue(MAX_VALUE);
setValue(DEFAULT_VALUE);
setBackgroundColor(BACKGROUND_COLOR);
setSeparatorColor(SEPARATOR_COLOR);
setTextColor(TEXT_COLOR);
setTextSize(TEXT_SIZE);
setWrapSelectorWheel(false);
setFocusability(false);
try {
Field f = NumberPicker.class.getDeclaredField("mInputText");
f.setAccessible(true);
EditText inputText = (EditText)f.get(this);
inputText.setFilters(new InputFilter[0]);
} catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) {
e.printStackTrace();
}
}
/**
* Init number picker by disabling focusability of edit text embedded inside the number picker
* We also override the edit text filter private attribute by using reflection as the formatter
* is
* still buggy while attempting to display the default value
* This is still an open Google @see <a href="https://code.google.com/p/android/issues/detail?id=35482#c9">issue</a>
* from 2012
*/
private void initView() {
setMinValue(MIN_VALUE);
setMaxValue(MAX_VALUE);
setValue(DEFAULT_VALUE);
setBackgroundColor(BACKGROUND_COLOR);
setSeparatorColor(SEPARATOR_COLOR);
setTextColor(TEXT_COLOR);
setTextSize(TEXT_SIZE);
setWrapSelectorWheel(false);
setFocusability(false);
try {
Field f = NumberPicker.class.getDeclaredField("mInputText");
f.setAccessible(true);
EditText inputText = (EditText) f.get(this);
inputText.setFilters(new InputFilter[0]);
} catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) {
e.printStackTrace();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.compose_message, container, false);
mSetPictureButton = (ImageButton)view.findViewById(R.id.compose_message_set_picture_button);
mPictureThumbnail = (ImageView)view.findViewById(R.id.compose_message_picture_thumbnail);
mContentEdit = (EditText)view.findViewById(R.id.compose_message_content_edit);
mSendButton = (ImageButton)view.findViewById(R.id.compose_message_send_button);
mSetPictureButton.setOnClickListener(this);
mPictureThumbnail.setVisibility(View.GONE);
mPictureThumbnail.setOnClickListener(this);
registerForContextMenu(mPictureThumbnail);
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(Protocol.MAX_MESSAGE_LENGTH);
mContentEdit.setFilters(filters);
mContentEdit.setOnEditorActionListener(this);
mSendButton.setOnClickListener(this);
return view;
}
private void showNewTweetDialog() {
final EditText tweetText = new EditText(this);
tweetText.setId(R.id.tweet_text);
tweetText.setSingleLine();
tweetText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
tweetText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(140)});
tweetText.setImeOptions(EditorInfo.IME_ACTION_DONE);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.label_what_is_happening);
builder.setPositiveButton(R.string.action_tweet, (dialog, which) -> presenter.tweet(tweetText.getText().toString()));
AlertDialog alert = builder.create();
alert.setView(tweetText, 64, 0, 64, 0);
alert.show();
tweetText.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
alert.getButton(DialogInterface.BUTTON_POSITIVE).callOnClick();
return true;
}
return false;
});
}
/**
* 限制中文字符的长度
*/
public static void lengthChineseFilter(@NonNull final EditText editText, final int length) {
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(length) {
public CharSequence filter(
CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
// 可以检查中文字符
boolean isChinese = StringUtil.checkNameChinese(source.toString());
if (!isChinese || dest.toString().length() >= length) {
return "";
}
return source;
}
};
editText.setFilters(filters);
}
private void addSpaceHandler(EditText et) {
final InputFilter filter = (source, start, end, dest, dstart, dend) -> {
for (int i = start; i < end; i++) {
if (Character.isWhitespace(source.charAt(i))) {
tryAddSuggestion();
return "";
}
}
return null;
};
et.setFilters(new InputFilter[]{filter});
}
public static void showSearchDialog(Context context, String keyWord, final OnSearchListener listener) {
int top = DisplayUtil.dip2px(context, 20f);
int left = DisplayUtil.dip2px(context, 26f);
FrameLayout frameLayout = new FrameLayout(context);
frameLayout.setLayoutParams(LayoutUtil.newFrameLayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
frameLayout.setPadding(left, top, left, 0);
int topPadding = DisplayUtil.dip2px(context, 10);
int leftPadding = DisplayUtil.dip2px(context, 4);
final EditText editText = new EditText(context);
editText.setPadding(leftPadding, topPadding, leftPadding, topPadding);
editText.setTextSize(15);
editText.setText(keyWord);
editText.setLayoutParams(LayoutUtil.newViewGroupParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setSingleLine(true);
editText.setFilters(new InputFilter[]{ new InputFilter.LengthFilter(50)});
editText.setHint("请输入搜索的关键字");
ViewUtil.setInputType(editText, com.sky.xposed.common.Constant.InputType.TEXT);
frameLayout.addView(editText);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("搜索");
builder.setView(frameLayout);
builder.setPositiveButton("搜索", (dialog, which) -> {
// 返回文本的内容
listener.onSearch(editText.getText().toString());
});
builder.setNegativeButton("取消", null);
builder.show();
}
public UDEditText setMaxLength(Integer maxLength) {
EditText view = getView();
if (view != null && maxLength != null) {
view.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
}
return this;
}
/**
* Inits the views.
*/
private void initViews() {
etInputCaptchaCode_farget = (EditText) findViewById(R.id.etInputCaptchaCode_farget);
btnReGetCaptchaCode_farget = (Button) findViewById(R.id.btnReGetCaptchaCode_farget);
CaptchaCode_Loading = (ProgressBar) findViewById(R.id.CaptchaCode_Loading);
//
tvDialog = (TextView) findViewById(R.id.tvDialog);
etName = (EditText) findViewById(R.id.etName);
etInputCode = (EditText) findViewById(R.id.etInputCode);
etInputPsw = (EditText) findViewById(R.id.etInputPsw);
etInputEmail = (EditText) findViewById(R.id.etInputEmail);
btnGetCode = (Button) findViewById(R.id.btnGetCode);
btnReGetCode = (Button) findViewById(R.id.btnReGetCode);
btnSure = (Button) findViewById(R.id.btnSure);
btnSureEmail = (Button) findViewById(R.id.btnSureEmail);
btnPhoneReset = (Button) findViewById(R.id.btnPhoneReset);
btnEmailReset = (Button) findViewById(R.id.btnEmailReset);
llInputMenu = (LinearLayout) findViewById(R.id.llInputMenu);
llInputPhone = (LinearLayout) findViewById(R.id.llInputPhone);
rlInputEmail = (RelativeLayout) findViewById(R.id.rlInputEmail);
rlDialog = (RelativeLayout) findViewById(R.id.rlDialog);
ivBack = (ImageView) findViewById(R.id.ivBack);
tbPswFlag = (ToggleButton) findViewById(R.id.tbPswFlag);
toogleUI(ui_statu.DEFAULT);
dialog = new ProgressDialog(this);
dialog.setMessage("处理中,请稍候...");
MyInputFilter filter = new MyInputFilter();
etInputPsw.setFilters(new InputFilter[] { filter });
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
} else if (item.getItemId() == R.id.edit_br_nav_done) {
(confirmationDialog = new ConfirmationDialog(this, HTMLUtil.getHTML(R.string.edit_br_profile_confirmation), () -> {
showManualOverlay();
List<ConfigurationBlock> blocks = new ArrayList<>();
blocks.add(nameBlock);
profileBlock.setProfileBlocks(FixedSizeProfileBlock.convertToRelative(profileBlocks));
blocks.add(profileBlock);
WriteConfigurationTaskRunner taskRunner = new WriteConfigurationTaskRunner(getServiceConnector(), blocks);
showLoadingIndicator();
taskRunner.fetch(this);
})).show();
} else if (item.getItemId() == R.id.edit_br_nav_edit_name) {
EditText editName = new EditText(this);
editName.setInputType(InputType.TYPE_CLASS_TEXT);
editName.setFilters(new InputFilter[] {new InputFilter.LengthFilter(21)});
editName.setText(nameBlock.getName());
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle(R.string.edit_name)
.setMessage(R.string.leave_empty_for_default_value)
.setView(editName)
.setPositiveButton(R.string.okay, ((dialog, which) -> {
nameBlock.setName(editName.getText().toString());
adjustTitle();
}))
.setNegativeButton(R.string.cancel, null)
.create();
alertDialog.show();
}
return super.onOptionsItemSelected(item);
}
/**
* Set a PinBox with all attributes
*
* @param editText to set attributes
*/
private void setStylePinBox(EditText editText) {
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(mNumberCharacters)});
if (mMaskPassword) {
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
else{
editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
if (mNativePinBox) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
//noinspection deprecation
editText.setBackgroundDrawable(new EditText(getContext()).getBackground());
} else {
editText.setBackground(new EditText(getContext()).getBackground());
}
} else {
editText.setBackgroundResource(mCustomDrawablePinBox);
}
if (mColorTextPinBoxes != PinViewSettings.DEFAULT_TEXT_COLOR_PIN_BOX) {
editText.setTextColor(mColorTextPinBoxes);
}
editText.setTextSize(PinViewUtils.convertPixelToDp(getContext(), mTextSizePinBoxes));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mms_enter);
final EditText qrMms=(EditText) findViewById(R.id.editTel);
final EditText qrText=(EditText) findViewById(R.id.editText1);
Button generate=(Button) findViewById(R.id.generate);
int maxLength = 15;
qrMms.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
int maxLength2 = 300;
qrText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength2)});
generate.setOnClickListener(new View.OnClickListener() {
String result;
@Override
public void onClick(View v) {
result = qrMms.getText().toString()+":"+qrText.getText().toString();
Intent i = new Intent(MmsEnterActivity.this, QrGeneratorDisplayActivity.class);
i.putExtra("gn", result);
i.putExtra("type", Contents.Type.MMS);
startActivity(i);
}
});
}
@Override
public View getView(View convertView) {
View view;
EditTextHolder holder;
if (convertView == null) {
ViewGroup rowRoot = (ViewGroup) inflater.inflate(R.layout.listview_row_integer_positive_or_zero, null);
TextView label = (TextView) rowRoot.findViewById(R.id.text_label);
EditText editText = (EditText) rowRoot.findViewById(R.id.edit_integer_pos_row);
editText.setFilters(new InputFilter[]{new InpFilter()});
EditTextWatcher watcher = new EditTextWatcher(field);
editText.addTextChangedListener(watcher);
holder = new EditTextHolder(label, editText, watcher);
rowRoot.setTag(holder);
view = rowRoot;
} else {
view = convertView;
holder = (EditTextHolder) view.getTag();
}
RowCosmetics.setTextLabel(field, holder.textLabel);
holder.textWatcher.setField(field);
holder.editText.addTextChangedListener(holder.textWatcher);
holder.editText.setText(field.getValue());
holder.editText.clearFocus();
holder.editText.setOnEditorActionListener(mOnEditorActionListener);
if(readOnly){
holder.editText.setEnabled(false);
} else {
holder.editText.setEnabled(true);
}
return view;
}
protected void setupEditText(EditText editText) {
editText.setInputType(InputType.TYPE_NULL);
editText.setFilters(filters);
editText.setOnTouchListener(touchListener);
editText.setTransformationMethod(PasswordTransformationMethod
.getInstance());
}
private SingleEmojiTrait(final EditText editText) {
this.editText = editText;
final List<InputFilter> filters = new ArrayList<>(Arrays.asList(editText.getFilters()));
filters.add(new OnlyEmojisInputFilter());
editText.setFilters(filters.toArray(new InputFilter[0]));
editText.addTextChangedListener(this);
}
public void setProhibitEmoji(EditText et)
{
InputFilter[] filters = { getInputFilterProhibitEmoji() ,getInputFilterProhibitSP()};
et.setFilters(filters);
}
protected void setupPinItem(EditText item){
item.setInputType(InputType.TYPE_NULL);
item.setFilters(filters);
item.setOnTouchListener(otl);
item.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
@Before
public void setUp() {
editText = new EditText(buildActivity(Activity.class).create().get());
editText.setFilters(new InputFilter[] {new MaxInputValidator(10)});
}
public void init() {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.layout_custom_dialog, null);
setContentView(view);
txtTitle = (TextView) view.findViewById(R.id.dialog_input_title);
editMessage = (EditText) view.findViewById(R.id.dialog_input_dialog_edit);
btnPostive = (TextView) view.findViewById(R.id.dialog_input_dialog_postive);
btnNegative = (TextView) view.findViewById(R.id.dialog_input_dialog_negative);
cacheCheck = (CheckBox) view.findViewById(R.id.dialog_input_check);
cacheCheck.setChecked(cache);
if (postiveButtonVisiable) {
btnPostive.setVisibility(View.VISIBLE);
} else {
btnPostive.setVisibility(View.GONE);
}
if (negativeButtonVisiable) {
btnNegative.setVisibility(View.VISIBLE);
} else {
btnNegative.setVisibility(View.GONE);
}
if (TextUtils.isEmpty(title)) {
txtTitle.setVisibility(View.GONE);
} else {
txtTitle.setVisibility(View.VISIBLE);
txtTitle.setText(title);
}
editMessage.setText(input);
if (input != null) {
editMessage.setSelection(input.length());
}
if (!TextUtils.isEmpty(hintInput)) {
editMessage.setHint(hintInput);
}
btnPostive.setText(postiveText);
btnNegative.setText(negativeText);
btnPostive.setOnClickListener(new OnButtonClickListener());
btnNegative.setOnClickListener(new OnButtonClickListener());
InputFilter[] filters = {new InputFilter.LengthFilter(maxLength)};
editMessage.setFilters(filters);
Window dialogWindow = getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
lp.width = (int) (d.widthPixels * 0.8); // 宽度设置为屏幕的0.8
dialogWindow.setAttributes(lp);
}