這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)怎么在Android中實(shí)現(xiàn)一個(gè)帶清除按鈕、密碼可見的輸入框,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都創(chuàng)新互聯(lián)公司擁有十余年成都網(wǎng)站建設(shè)工作經(jīng)驗(yàn),為各大企業(yè)提供做網(wǎng)站、成都做網(wǎng)站服務(wù),對(duì)于網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、重慶App定制開發(fā)、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、程序開發(fā)、網(wǎng)站優(yōu)化(SEO優(yōu)化)、微網(wǎng)站、申請(qǐng)域名等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了很多網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營(yíng)銷經(jīng)驗(yàn),集策劃、開發(fā)、設(shè)計(jì)、營(yíng)銷、管理等網(wǎng)站化運(yùn)作于一體,具備承接各種規(guī)模類型的網(wǎng)站建設(shè)項(xiàng)目的能力。我的這個(gè)自帶清除、小眼睛的控件,是一個(gè)自定義控件,控件名稱ClearEditText.class,繼承RelativeLayout并有布局引用。在拷貝到項(xiàng)目里面后,使用時(shí)候只需要與普通輸入框控件一樣,進(jìn)行相關(guān)的配置即可,部分屬性需要使用自定義屬性,具體見代碼。
拷貝ClearEditText.class類到自己的項(xiàng)目里,再考拷貝它相關(guān)的布局文件、自定義屬性、默認(rèn)圖片即可,下面貼出ClearEditText主要代碼:
public class ClearEditText extends RelativeLayout { private ImageView ivLeftIcon; private EditText myEdie; private ImageView ivEditClean; private ImageView ivEditEye; private boolean isChecked = true; private Context mContext; private TypedArray mTypedArray; private boolean showClean = true;//清空?qǐng)D標(biāo)是否顯示,true:顯示 private boolean showEye = false;//密碼可見圖標(biāo)是否顯示,true:顯示 private int drawableLeft = -1;//是否顯示輸入框左側(cè)圖片 private int drawableEyeOpen = R.drawable.clear_icon_eye_open;//可以看見密碼小眼睛樣式 private int drawableEyeClose = R.drawable.clear_icon_eye_close;//不可見密碼小眼睛樣式 private int drawableClean = R.drawable.clear_icon_close;//清除按鈕圖片 private int cleanPadding = 0;//清除按鈕padding邊距 private String hintStr; private String textStr; private int mTextColorHint = Color.BLACK; private int mTextColor = Color.BLACK; private int mTextSize = -1; private int mMaxLength = 2000; private int mMaxLines = 1; private int mInputType = 0;//輸入類型,就做了不限制、數(shù)字、文本密碼三種 private boolean isInput = false;//輸入1個(gè)字符后更改狀態(tài)為true,保證小眼睛移動(dòng)一次 private boolean isHideClean = false;//輸入字符后,清除了需要小眼睛歸為,清除按鈕隱藏 private int ivWidth = 45;//關(guān)閉按鈕的寬度 public ClearEditText(Context context) { super(context); mContext = context; initView(); } public ClearEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; mTypedArray = mContext.obtainStyledAttributes(attrs, R.styleable.myEditText); initView(); } public ClearEditText(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; mTypedArray = mContext.obtainStyledAttributes(attrs, R.styleable.myEditText); showClean = mTypedArray.getBoolean(R.styleable.myEditText_showClean, showClean); drawableClean = mTypedArray.getResourceId(R.styleable.myEditText_drawableClean, drawableClean); cleanPadding = mTypedArray.getDimensionPixelSize(R.styleable.myEditText_cleanPadding, cleanPadding); showEye = mTypedArray.getBoolean(R.styleable.myEditText_showEye, showEye); drawableLeft = mTypedArray.getResourceId(R.styleable.myEditText_drawableLeft, -1); drawableEyeClose = mTypedArray.getResourceId(R.styleable.myEditText_drawableEyeClose, drawableEyeClose); drawableEyeOpen = mTypedArray.getResourceId(R.styleable.myEditText_drawableEyeOpen, drawableEyeOpen); hintStr = mTypedArray.getString(R.styleable.myEditText_hint); textStr = mTypedArray.getString(R.styleable.myEditText_text); mTextColorHint = mTypedArray.getColor(R.styleable.myEditText_textColorHint, mTextColorHint); mTextColor = mTypedArray.getColor(R.styleable.myEditText_textColor, mTextColor); mTextSize = mTypedArray.getDimensionPixelSize(R.styleable.myEditText_textSize, mTextSize); mMaxLength = mTypedArray.getInteger(R.styleable.myEditText_maxLength, mMaxLength); mMaxLines = mTypedArray.getDimensionPixelSize(R.styleable.myEditText_maxLines, mMaxLines); mInputType = mTypedArray.getInteger(R.styleable.myEditText_inputType, mInputType); mTypedArray.recycle(); initView(); } // 初始化視圖 private void initView() { View view = View.inflate(getContext(), R.layout.clear_layout_view, null); ivLeftIcon = (ImageView) view.findViewById(R.id.iv_edit_left_icon); myEdie = (EditText) view.findViewById(R.id.view_edit_show); ivEditClean = (ImageView) view.findViewById(R.id.iv_edit_clean); ivEditEye = (ImageView) view.findViewById(R.id.iv_edit_eye); myEdie.setHint(hintStr); myEdie.setHintTextColor(mTextColorHint); myEdie.setText(textStr); myEdie.setTextColor(mTextColor); myEdie.setMaxLines(mMaxLines); myEdie.setFilters(new InputFilter[]{new InputFilter.LengthFilter(mMaxLength)}); if (mTextSize != -1) { myEdie.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize); } else { myEdie.setTextSize(15); } if (mInputType == 1) { myEdie.setInputType(InputType.TYPE_CLASS_NUMBER); } else if (mInputType == 2) { myEdie.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT); } else { myEdie.setInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL | InputType.TYPE_CLASS_TEXT); } if (showEye) { myEdie.setTransformationMethod(new AsteriskPasswordTransformationMethod()); } if (showClean && showEye) { int left = myEdie.getPaddingLeft(); int top = myEdie.getPaddingTop(); int bottom = myEdie.getPaddingBottom(); myEdie.setPadding(left, top, Utils.dp2px(mContext, 90), bottom); } else if (!showClean && !showEye) { int left = myEdie.getPaddingLeft(); int top = myEdie.getPaddingTop(); int right = myEdie.getPaddingRight(); int bottom = myEdie.getPaddingBottom(); myEdie.setPadding(left, top, right, bottom); } else { int left = myEdie.getPaddingLeft(); int top = myEdie.getPaddingTop(); int bottom = myEdie.getPaddingBottom(); myEdie.setPadding(left, top, Utils.dp2px(mContext, 45), bottom); } myEdie.addTextChangedListener(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) { if (s.length() > 0) { isHideClean = false; } } @Override public void afterTextChanged(Editable s) { if (s.length() > 0 && !isInput) {//輸入字符大于0且只有一個(gè)字符時(shí)候顯示清除按鈕動(dòng)畫,小眼睛移動(dòng)出位置給清除按鈕使用 showEditClean(); moveEditEye(); isInput = true; } else if (s.length() == 0) {//無字符小眼睛歸位 UndoEditEye(); } if (s.length() == 0 & !isHideClean) { hideEditClean(); isHideClean = true; isInput = false; } if (onEditInputListener != null) { onEditInputListener.input(getText()); } } }); setEditClean(showClean); ivEditClean.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { myEdie.setText(""); } }); ivEditClean.setImageResource(drawableClean); ivEditClean.setPadding(cleanPadding, cleanPadding, cleanPadding, cleanPadding); setEditEye(showEye); ivEditEye.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (isChecked) { // 輸入一個(gè)對(duì)用戶可見的密碼 myEdie.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); myEdie.setSelection(getText().length()); ivEditEye.setImageResource(drawableEyeOpen); isChecked = false; } else { // 輸入一個(gè)對(duì)用戶不可見的密碼 myEdie.setTransformationMethod(new AsteriskPasswordTransformationMethod()); myEdie.setSelection(getText().length()); ivEditEye.setImageResource(drawableEyeClose); isChecked = true; } } }); if (drawableLeft != -1) { ivLeftIcon.setVisibility(View.VISIBLE); ivLeftIcon.setImageResource(drawableLeft); } else { ivLeftIcon.setVisibility(View.GONE); } view.setLayoutParams(new LayoutParams(ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.WRAP_CONTENT)); addView(view); } //密碼不可見時(shí)候,使用*替換密碼 public class AsteriskPasswordTransformationMethod extends PasswordTransformationMethod { @Override public CharSequence getTransformation(CharSequence source, View view) { return new PasswordCharSequence(source); } private class PasswordCharSequence implements CharSequence { private CharSequence mSource; public PasswordCharSequence(CharSequence source) { mSource = source; // Store char sequence } public char charAt(int index) { return '*'; // This is the important part } public int length() { return mSource.length(); // Return default } public CharSequence subSequence(int start, int end) { return mSource.subSequence(start, end); // Return default } } } public String getText() { return myEdie.getText().toString().trim(); } public void setText(String text) { myEdie.setText(text); } //代碼設(shè)置是否顯示清除按鈕 public void setEditClean(boolean isCanClose) { showClean = isCanClose; } //代碼設(shè)置是否顯示小眼睛 public void setEditEye(boolean isCanSee) { showEye = isCanSee; if (showEye == true) { ivEditEye.setVisibility(View.VISIBLE); } else { ivEditEye.setVisibility(View.GONE); } } private void showEditClean() { if (showClean == true) { AnimationUtils.showAndHiddenCenterAnimation(ivEditClean, AnimationUtils.AnimationState.STATE_SHOW, 500); } } private void hideEditClean() { if (showClean == true) { AnimationUtils.showAndHiddenCenterAnimation(ivEditClean, AnimationUtils.AnimationState.STATE_HIDDEN, 500); } } private void moveEditEye() { if (showEye) { ObjectAnimator.ofFloat(ivEditEye, "translationX", -Utils.dp2px(mContext, ivWidth)).setDuration(500).start(); } } private void UndoEditEye() { if (showEye) { ObjectAnimator.ofFloat(ivEditEye, "translationX", 0).setDuration(500).start(); } } public OnEditInputListener onEditInputListener; public void setOnEditInputListener(OnEditInputListener listener) { onEditInputListener = listener; } //輸入監(jiān)聽 public interface OnEditInputListener { void input(String content); } }
下面是在xml布局時(shí)候,如何使用ClearEditText的示例,部分自定義屬性使用xmlns:app="http://schemas.android.com/apk/res-auto"引用調(diào)用。app:showEye屬性true-顯示小眼睛,false-關(guān)閉小眼睛。
上述就是小編為大家分享的怎么在Android中實(shí)現(xiàn)一個(gè)帶清除按鈕、密碼可見的輸入框了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。