欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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); //使得父類獲得和子控件相同的狀態(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android加載Assets目錄中Xml布局文件

    Android加載Assets目錄中Xml布局文件

    這篇文章主要為大家詳細(xì)介紹了Android加載Assets目錄中Xml布局文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • android中Intent傳值與Bundle傳值的區(qū)別詳解

    android中Intent傳值與Bundle傳值的區(qū)別詳解

    本篇文章是對(duì)android中Intent傳值與Bundle傳值的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • Android四大組件之Activity詳解

    Android四大組件之Activity詳解

    今天小編就為大家分享一篇關(guān)于Android四大組件之Activity詳解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • 淺談android @id和@+id的區(qū)別

    淺談android @id和@+id的區(qū)別

    這篇文章主要介紹了淺談android @id和@+id的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • android導(dǎo)入第三方j(luò)ar包報(bào)錯(cuò) 如何正確導(dǎo)入jar包

    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自定義桌面功能代碼實(shí)現(xiàn)

    Android自定義桌面功能代碼實(shí)現(xiàn)

    android自定義桌面其實(shí)很簡(jiǎn)單,看一個(gè)例子就明白了
    2013-11-11
  • Android UI手機(jī)信息頁(yè)面設(shè)計(jì)

    Android UI手機(jī)信息頁(yè)面設(shè)計(jì)

    這篇文章主要為大家詳細(xì)介紹了Android UI手機(jī)信息頁(yè)面的設(shè)計(jì)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android Tabhost使用方法詳解

    Android Tabhost使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Android Tabhost使用方法,如何利用TabHost 實(shí)現(xiàn)tab視圖,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例

    Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例

    本篇文章主要介紹了Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-11-11
  • Android實(shí)現(xiàn)自定義飄雪效果

    Android實(shí)現(xiàn)自定義飄雪效果

    隨著冬季的腳步越來(lái)越遠(yuǎn),南方的我今年就看了一場(chǎng)雪,下一場(chǎng)雪遙遙無(wú)期,那我們來(lái)實(shí)現(xiàn)一個(gè)自定義的 View,它能模擬雪花飄落的景象,所以本文給大家介紹了基于Android實(shí)現(xiàn)自定義飄雪效果,感興趣的朋友可以參考下
    2024-01-01

最新評(píng)論