最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義密碼輸入EditTextLayout

 更新時間:2018年08月21日 11:49:06   作者:showdy  
這篇文章主要為大家詳細介紹了Android自定義密碼輸入EditTextLayout,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文為大家分享了Android自定義密碼輸入的具體代碼,供大家參考,具體內容如下

布局

<?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);
      //關鍵點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);
      //關鍵點2
      setAddStatesFromChildren(true); //使得父類獲得和子控件相同的狀態(tài)
      mEditText.addTextChangedListener(this);
      mEditText.setOnFocusChangeListener(this);
      mDeleteIcon.setOnClickListener(this);
      mShiftIcon.setOnCheckedChangeListener(this);
      //設置默認狀態(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());
  }

}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android 清除SharedPreferences 產生的數據(實例代碼)

    Android 清除SharedPreferences 產生的數據(實例代碼)

    項目是要保存上次文件播放的位置,我使用SharedPreferences來保存,鍵值對分別是文件路徑和當時播放的位置
    2013-11-11
  • 分享安裝Android Studio3.6的經驗教訓

    分享安裝Android Studio3.6的經驗教訓

    這篇文章主要介紹了Android Studio3.6的安裝錯誤問題及解決方法,非常值得大家參考,現(xiàn)把整個過程分享到腳本之家平臺,需要的朋友參考下吧
    2020-02-02
  • Android手勢ImageView三部曲 第一部

    Android手勢ImageView三部曲 第一部

    這篇文章主要為大家詳細介紹了Android手勢ImageView三部曲的第一部,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android自定義實現(xiàn)側滑菜單效果

    Android自定義實現(xiàn)側滑菜單效果

    這篇文章主要為大家詳細介紹了Android自定義實現(xiàn)側滑菜單效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Android實現(xiàn)屏幕鎖定源碼詳解

    Android實現(xiàn)屏幕鎖定源碼詳解

    本篇文章主要介紹了Android實現(xiàn)屏幕鎖定源碼詳解,屏幕鎖定是一個很有用的功能,有需要的可以了解一下。
    2016-10-10
  • Android多媒體教程之播放視頻的四種方法

    Android多媒體教程之播放視頻的四種方法

    這篇文章主要給大家介紹了關于Android多媒體教程之播放視頻的四種方法,分別是通過intent的方式,調用系統(tǒng)自帶的播放器、使用VideoView、MediaPlayer + SurfaceView及MediaPlayer + TextureView等方法,需要的朋友們可以參考學習。
    2017-06-06
  • Android adb 出錯解決方法

    Android adb 出錯解決方法

    本文主要介紹Android 中的adb,相信大家在開發(fā)過程中經常使用adb進行調試,這里主要說明adb 出錯問題解決方法,希望能幫助有需要的同學
    2016-07-07
  • Android中Webview使用全面詳解

    Android中Webview使用全面詳解

    Android WebView是一個用于在應用程序中顯示網頁內容的組件,它可以加載網頁并在應用程序內部顯示,而不是調用系統(tǒng)瀏覽器,這篇文章主要給大家介紹了關于Android中Webview使用的相關資料,需要的朋友可以參考下
    2024-07-07
  • android shape的使用及漸變色、分割線、邊框、半透明陰影

    android shape的使用及漸變色、分割線、邊框、半透明陰影

    這篇文章主要介紹了android shape的使用及漸變色、分割線、邊框、半透明陰影,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • Android中的Notification機制深入理解

    Android中的Notification機制深入理解

    這篇文章主要給大家介紹了關于Android中Notification機制的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-02-02

最新評論

上栗县| 焉耆| 旬邑县| 乐清市| 滨海县| 陇南市| 普宁市| 辽阳县| 普定县| 武邑县| 剑阁县| 高邑县| 安丘市| 汉源县| 新巴尔虎右旗| 政和县| 旬邑县| 湘潭市| 札达县| 陵水| 区。| 贡山| 潞城市| 六安市| 四会市| 潼关县| 天门市| 东兴市| 镇远县| 铜梁县| 九寨沟县| 富民县| 晴隆县| 昆山市| 宝坻区| 稷山县| 大新县| 东丽区| 阿鲁科尔沁旗| 茶陵县| 绥中县|