国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android自定義密碼輸入EditTextLayout

2019-10-21 21:40:45
字體:
供稿:網(wǎng)友

本文為大家分享了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> * 一個用于密碼輸入的自定義控件 */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); //使得父類獲得和子控件相同的狀態(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());  }}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 洞头县| 建湖县| 宜兴市| 桦甸市| 巴彦县| 观塘区| 连江县| 紫云| 靖宇县| 青川县| 石柱| 留坝县| 金湖县| 长武县| 呼和浩特市| 胶南市| 肃北| 巴中市| 枣强县| 龙井市| 龙井市| 通榆县| 香河县| 晴隆县| 广德县| 黄骅市| 西宁市| 南川市| 辰溪县| 扶沟县| 洱源县| 神农架林区| 客服| 青铜峡市| 太湖县| 湟中县| 汉中市| 平邑县| 温宿县| 泰兴市| 丽水市|