Android自定義密碼輸入EditTextLayout
更新時(shí)間:2018年08月21日 11:49:06 作者:showdy
這篇文章主要為大家詳細(xì)介紹了Android自定義密碼輸入EditTextLayout,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文為大家分享了Android自定義密碼輸入的具體代碼,供大家參考,具體內(nèi)容如下
布局
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/delete"
android:layout_width="30dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:scaleType="center"
android:src="@drawable/ico_delete"/>
<CheckBox
android:id="@+id/ck_shift"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pwd_selector"
android:button="@null"/>
</merge>
用于密碼輸入的自定義控件
/**
* Created by showdy on 2017/3/15.
* <p>
* 一個(gè)用于密碼輸入的自定義控件
*/
public class PwdEditLayout extends LinearLayout implements TextWatcher, View.OnFocusChangeListener,
View.OnClickListener, CompoundButton.OnCheckedChangeListener {
private ImageView mDeleteIcon;
private CheckBox mShiftIcon;
private EditText mEditText;
public PwdEditLayout(Context context) {
this(context, null);
}
public PwdEditLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public PwdEditLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOrientation(HORIZONTAL);
setCustomBackground();
}
private void setCustomBackground() {
GradientDrawable gd = new GradientDrawable();
gd.setCornerRadius(TypedValue.applyDimension(COMPLEX_UNIT_DIP, 4, Resources.getSystem().getDisplayMetrics()));
gd.setStroke((int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, 1, Resources.getSystem().getDisplayMetrics()), Color.BLUE);
if (Build.VERSION.SDK_INT < 16) {
this.setBackgroundDrawable(gd);
} else {
this.setBackground(gd);
}
}
/**
* Called when a new child is aded to this ViewGroup. Overrides should always
* call super.onViewAdded.
*
* @param child the added child view
*/
@Override
public void onViewAdded(View child) {
super.onViewAdded(child);
if (child instanceof EditText) {
if (getChildCount() != 1) {
throw new IllegalArgumentException("Only one EditText can be added in this layout.");
}
mEditText = (EditText) child;
mEditText.setBackgroundColor(Color.TRANSPARENT);
//關(guān)鍵點(diǎn)1
LayoutInflater.from(getContext()).inflate(R.layout.layout_edittext_pwd, this, true);
mDeleteIcon = (ImageView) findViewById(R.id.delete);
mShiftIcon = (CheckBox) findViewById(R.id.ck_shift);
//關(guān)鍵點(diǎn)2
setAddStatesFromChildren(true); //使得父類(lèi)獲得和子控件相同的狀態(tài)
mEditText.addTextChangedListener(this);
mEditText.setOnFocusChangeListener(this);
mDeleteIcon.setOnClickListener(this);
mShiftIcon.setOnCheckedChangeListener(this);
//設(shè)置默認(rèn)狀態(tài)---刪除按鈕和是否顯示密碼
mShiftIcon.setChecked(false);
updateDeleteIcon(mEditText.getText().toString(), mEditText.isFocused());
updateShowPassword(mShiftIcon.isChecked());
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
updateDeleteIcon(s.toString(), mEditText.isFocused());
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
updateDeleteIcon(mEditText.getText().toString(), hasFocus);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.delete:
mEditText.setText("");
break;
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
updateShowPassword(isChecked);
}
/**
* 用于是否顯示密碼
*
* @param password
* @param focused
*/
private void updateDeleteIcon(String password, boolean focused) {
if (!TextUtils.isEmpty(password) && focused) {
mDeleteIcon.setVisibility(VISIBLE);
} else {
mDeleteIcon.setVisibility(INVISIBLE);
}
}
/**
* 用于控制是否顯示密碼
*
* @param checked
*/
private void updateShowPassword(boolean checked) {
if (checked) {
mEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
mEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
mEditText.setSelection(mEditText.getText().toString().length());
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android 登錄頁(yè)面的實(shí)現(xiàn)代碼(密碼顯示隱藏、EditText 圖標(biāo)切換、限制輸入長(zhǎng)度)
- Android EditText隨輸入法一起移動(dòng)并懸浮在輸入法之上的示例代碼
- Android EditText每4位自動(dòng)添加空格效果
- Android EditText追加空格、限制字符等方法示例
- Android EditText長(zhǎng)按菜單中分享功能的隱藏方法
- Android中多個(gè)EditText輸入效果的解決方式
- Android實(shí)現(xiàn)微信朋友圈評(píng)論EditText效果
- Android EditText監(jiān)聽(tīng)回車(chē)鍵并處理兩次回調(diào)問(wèn)題
- Android實(shí)現(xiàn)EditText添加下劃線(xiàn)
- Android EditText限制輸入整數(shù)和小數(shù)的位數(shù)的方法示例
- Android使用EditText小技巧匯總
相關(guān)文章
android中Intent傳值與Bundle傳值的區(qū)別詳解
本篇文章是對(duì)android中Intent傳值與Bundle傳值的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
android導(dǎo)入第三方j(luò)ar包報(bào)錯(cuò) 如何正確導(dǎo)入jar包
怎樣在android平臺(tái)上使用第三方j(luò)ar包,為什么我在引入了,編譯時(shí)沒(méi)有錯(cuò)誤,運(yùn)行時(shí)就有錯(cuò)誤,報(bào)無(wú)法實(shí)例化錯(cuò)誤,請(qǐng)問(wèn)這是什么原因,本文給于解決方法,需要了解的朋友可以參考下2012-12-12
Android UI手機(jī)信息頁(yè)面設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了Android UI手機(jī)信息頁(yè)面的設(shè)計(jì)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例
本篇文章主要介紹了Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-11-11

