Android中怎么通過自定義EditText實現(xiàn)淘寶登錄功能-創(chuàng)新互聯(lián)

本篇文章為大家展示了Android中怎么通過自定義EditText實現(xiàn)淘寶登錄功能,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

目前創(chuàng)新互聯(lián)已為上1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計、臨渭區(qū)網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

整體布局UI:

 <com.example.zdyedittext.ClearEditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="35dp"
    android:layout_alignTop="@+id/imageView1"
    android:layout_marginLeft="17dp"
    android:layout_toRightOf="@+id/imageView1"
    android:background="@android:color/white"
    android:ems="10"
    android:hint="手機號"
    android:padding="8dp"
    android:singleLine="true" />

  <com.example.zdyedittext.ClearEditText
    android:id="@+id/et_pass_word"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="密碼"
    android:background="@android:color/white"
    android:password="true"
    android:padding="8dp"
    android:singleLine="true" />

自定義EditText類

由于自定義EditText理所當然要集成EditText

public class ClearEditText extends EditText

然后添加構(gòu)造方法,是為了能在XML中能夠引用。

 public ClearEditText(Context context, AttributeSet attrs) {  
    this(context, attrs, android.R.attr.editTextStyle); 
  }

接下來就是設(shè)置自己的EditText的樣式,添加自己想要的樣式。具體是在init()方法中實現(xiàn)。

 public ClearEditText(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    init(); 
  }

init()方法的實現(xiàn)過程:[2]參數(shù)為:dr.mDrawableRight,定義刪除按鈕是在EditText的右邊,設(shè)置圖標的左上右下:mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());

private void init() { 
    // 獲取EditText的DrawableRight,假如沒有設(shè)置我們就使用默認的圖片 
    mClearDrawable = getCompoundDrawables()[2]; 
    if (mClearDrawable == null) {  
      mClearDrawable = getResources().getDrawable(R.drawable.del);//R.drawable.del刪除圖標的圖片 
    } 
    mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight()); 

    //設(shè)置圖標的左上右下
    // 默認設(shè)置隱藏圖標 
    setClearIconVisible(false); 
    // 設(shè)置焦點改變的監(jiān)聽 
    setOnFocusChangeListener(this); 
    // 設(shè)置輸入框里面內(nèi)容發(fā)生改變的監(jiān)聽 
    addTextChangedListener(this); 
  }

由于不能直接給EditText設(shè)置監(jiān)聽事件,所以采用記錄點擊位置來模擬點擊事件,只記錄了魚圖標的左右點擊。

public boolean onTouchEvent(MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_UP) { 
      if (getCompoundDrawables()[2] != null) { 

        boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight()) && (event.getX() < ((getWidth() - getPaddingRight()))); 

        if (touchable) { 
          this.setText(""); 
        } 
      } 
    } 

    return super.onTouchEvent(event); 
  }


判斷輸入框中是否有文字,動態(tài)設(shè)置刪除圖標的顯示和隱藏。

public void onFocusChange(View v, boolean hasFocus) { 
    this.hasFoucs = hasFocus; 
    if (hasFocus) { 
      setClearIconVisible(getText().length() > 0); 
    } else { 
      setClearIconVisible(false); 
    } 
  }

如果輸入框中有文字 那么久繪制刪除圖標

protected void setClearIconVisible(boolean visible) { 
    Drawable right = visible ? mClearDrawable : null; 
    setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]); 
  }

 當輸入框內(nèi)容發(fā)生變化的時候動態(tài)改變刪除圖標

 public void onTextChanged(CharSequence s, int start, int count, int after) { 
    if (hasFoucs) { 
      setClearIconVisible(s.length() > 0); 
    } 
  }

至此就完成了:當屬框中沒有文本的時候 刪除圖標隱藏 當有文本輸入的時候,刪除圖標顯示,點擊刪除圖標,清空文本內(nèi)容。

自定義InputType返回為”*”

設(shè)置密碼樣式要繼承PasswordTransformationMethod這個類然后實現(xiàn)CharSequence方法去修改CharAt的返回值為“*”即可。

 private class PasswordCharSequence implements CharSequence {
    private CharSequence mSource;
    public PasswordCharSequence(CharSequence source) {
      mSource = source; // Store char sequence
    }
    這里用于修改InputType的返回樣式
    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
    }
  }

然后在主程序中初始化控件,在布局中設(shè)置android:password=”true”這一行代碼,以便在代碼中動態(tài)設(shè)置密碼輸入的返回樣式。

et_pass_word = (ClearEditText) findViewById(R.id.et_pass_word);
et_pass_word.setTransformationMethod(new EditTextBgToStar());

上述內(nèi)容就是Android中怎么通過自定義EditText實現(xiàn)淘寶登錄功能,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當前題目:Android中怎么通過自定義EditText實現(xiàn)淘寶登錄功能-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://muchs.cn/article48/ddophp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、服務(wù)器托管、定制網(wǎng)站、云服務(wù)器電子商務(wù)、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)