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

Android如何自定義EditText下劃線?

 更新時間:2016年01月14日 11:54:30   作者:OneAPM  
Android如何自定義EditText下劃線?本文教大家利用Android實現(xiàn)自定義的EditText下劃線,感興趣的小伙伴們可以參考一下

曾經(jīng)做過一個項目,其中登錄界面的交互令人印象深刻。交互設(shè)計師給出了一個非常作的設(shè)計,要求做出包含根據(jù)情況可變色的下劃線,左側(cè)有可變圖標(biāo),右側(cè)有可變刪除標(biāo)志的輸入框,如圖

記錄制作過程:

第一版本

public class LineEditText extends EditText {

private Paint mPaint;
private int color;
public static final int STATUS_FOCUSED = 1;
public static final int STATUS_UNFOCUSED = 2;
public static final int STATUS_ERROR = 3;
private int status = 2;
private Drawable del_btn;
private Drawable del_btn_down;
private int focusedDrawableId = R.drawable.user_select;// 默認(rèn)的
private int unfocusedDrawableId = R.drawable.user;
private int errorDrawableId = R.drawable.user_error;
Drawable left = null;
private Context mContext;
public LineEditText(Context context) {

 super(context);
 mContext = context;
 init();
}
public LineEditText(Context context, AttributeSet attrs) {

 super(context, attrs);
 mContext = context;
 init();

}
public LineEditText(Context context, AttributeSet attrs, int defStryle) {

 super(context, attrs, defStryle);
 mContext = context;
 TypedArray a = context.obtainStyledAttributes(attrs,
   R.styleable.lineEdittext, defStryle, 0);
 focusedDrawableId = a.getResourceId(
   R.styleable.lineEdittext_drawableFocus, R.drawable.user_select);
 unfocusedDrawableId = a.getResourceId(
   R.styleable.lineEdittext_drawableUnFocus, R.drawable.user);
 errorDrawableId = a.getResourceId(
   R.styleable.lineEdittext_drawableError, R.drawable.user_error);
 a.recycle();
 init();
}
/** * 2014/7/31 * * @author Aimee.ZHANG */

private void init() {
 mPaint = new Paint();
 // mPaint.setStyle(Paint.Style.FILL);
 mPaint.setStrokeWidth(3.0f);
 color = Color.parseColor("#bfbfbf");
 setStatus(status);
 del_btn = mContext.getResources().getDrawable(R.drawable.del_but_bg);
 del_btn_down = mContext.getResources().getDrawable(R.drawable.del_but_bg_down);
 addTextChangedListener(new TextWatcher() {

  @Override
  public void onTextChanged(CharSequence arg0, int arg1, int arg2,
    int arg3) {
  }

  @Override
  public void beforeTextChanged(CharSequence arg0, int arg1,
    int arg2, int arg3) {
  }

  @Override
  public void afterTextChanged(Editable arg0) {
   setDrawable();
  }
 });
 setDrawable();
}

@Override
protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 mPaint.setColor(color);
 canvas.drawLine(0, this.getHeight() - 1, this.getWidth(),
   this.getHeight() - 1, mPaint);
}

// 刪除圖片
private void setDrawable() {
 if (length() < 1) {
  setCompoundDrawablesWithIntrinsicBounds(left, null, del_btn, null);
 } else {
  setCompoundDrawablesWithIntrinsicBounds(left, null, del_btn_down,null);
 }
}

// 處理刪除事件
@Override
public boolean onTouchEvent(MotionEvent event) {
 if (del_btn_down != null && event.getAction() == MotionEvent.ACTION_UP) {
  int eventX = (int) event.getRawX();
  int eventY = (int) event.getRawY();
  Log.e("eventXY", "eventX = " + eventX + "; eventY = " + eventY); 
  Rect rect = new Rect();
  getGlobalVisibleRect(rect);
  rect.left = rect.right - 50;
  if (rect.contains(eventX, eventY))
  setText("");
 }
 return super.onTouchEvent(event);
}

public void setStatus(int status) {
 this.status = status;


 if (status == STATUS_ERROR) {
  try {
   left = getResources().getDrawable(errorDrawableId);
  } catch (NotFoundException e) {
   e.printStackTrace();
  }
  setColor(Color.parseColor("#f57272"));
 } else if (status == STATUS_FOCUSED) {
  try {
   left = getResources().getDrawable(focusedDrawableId);
  } catch (NotFoundException e) {
   e.printStackTrace();
  }
  setColor(Color.parseColor("#5e99f3"));
 } else {
  try {
   left = getResources().getDrawable(unfocusedDrawableId);
  } catch (NotFoundException e) {
   e.printStackTrace();
  }
  setColor(Color.parseColor("#bfbfbf"));
 }
 if (left != null) {
// left.setBounds(0, 0, 30, 40);

// this.setCompoundDrawables(left, null, null, null);

  setCompoundDrawablesWithIntrinsicBounds(left,null,del_btn,null);
 }
 postInvalidate();
}

public void setLeftDrawable(int focusedDrawableId, int unfocusedDrawableId,
  int errorDrawableId) {
 this.focusedDrawableId = focusedDrawableId;
 this.unfocusedDrawableId = unfocusedDrawableId;
 this.errorDrawableId = errorDrawableId;
 setStatus(status);
}

@Override
protected void onFocusChanged(boolean focused, int direction,
  Rect previouslyFocusedRect) {
 super.onFocusChanged(focused, direction, previouslyFocusedRect);
 if (focused) {
  setStatus(STATUS_FOCUSED);
 } else {
  setStatus(STATUS_UNFOCUSED);
 }
}

@Override
protected void finalize() throws Throwable {
 super.finalize();
};

public void setColor(int color) {
 this.color = color;
 this.setTextColor(color);
 invalidate();
}
}

效果圖:


代碼解釋:

變量名 STATUS_FOCUSED,STATUS_UNFOCUSED,STATUS_ERROR 標(biāo)示了三種狀態(tài),選中狀況為藍(lán)色,未選中狀態(tài)為灰色,錯誤狀態(tài)為紅色。 focusedDrawableId unfocusedDrawableId errorDrawableId 存放三種狀態(tài)的圖片,放置于最左側(cè)。

canvas.drawLine(0, this.getHeight() - 1, this.getWidth(),this.getHeight() - 1, mPaint); //畫editText 最下方的線 setCompoundDrawablesWithIntrinsicBounds(left, null, del_btn, null); //放置左邊的和右邊的圖片(左,上,右,下) 相當(dāng)于 android:drawableLeft="" android:drawableRight=""

1、onTouchEvent 當(dāng)手機點擊時,第一個先執(zhí)行的函數(shù),當(dāng)點擊右側(cè)刪除圖標(biāo)是清空 edittext
2、setStatus 根據(jù)不同的狀態(tài),左邊的圖片不一樣

存在的問題: 這版本雖然基本功能已經(jīng)實現(xiàn),但是不符合需求,設(shè)計中要求文本框中無文字時,右側(cè)刪除按鈕不顯示,不點擊刪除按鈕,刪除按鈕要保持灰色,點擊時才可以變藍(lán)色。

因此有了第二個版本

public class LineEditText extends EditText implements TextWatcher, <br /> OnFocusChangeListener{

private Paint mPaint;
private int color;
public static final int STATUS_FOCUSED = 1;
public static final int STATUS_UNFOCUSED = 2;
public static final int STATUS_ERROR = 3;
private int status = 2;
private Drawable del_btn;
private Drawable del_btn_down;
private int focusedDrawableId = R.drawable.user_select;// 默認(rèn)的
private int unfocusedDrawableId = R.drawable.user;
private int errorDrawableId = R.drawable.user_error;
Drawable left = null;
private Context mContext;
/** 
 * 是否獲取焦點,默認(rèn)沒有焦點 
 */ 
private boolean hasFocus = false; 
/** 
 * 手指抬起時的X坐標(biāo) 
 */ 
private int xUp = 0; 

public LineEditText(Context context) {
 super(context);
 mContext = context;
 init();
}

public LineEditText(Context context, AttributeSet attrs) {
 super(context, attrs);
 mContext = context;
 init();

}

public LineEditText(Context context, AttributeSet attrs, int defStryle) {
 super(context, attrs, defStryle);
 mContext = context;
 TypedArray a = context.obtainStyledAttributes(attrs,
   R.styleable.lineEdittext, defStryle, 0);
 focusedDrawableId = a.getResourceId(
   R.styleable.lineEdittext_drawableFocus, R.drawable.user_select);
 unfocusedDrawableId = a.getResourceId(
   R.styleable.lineEdittext_drawableUnFocus, R.drawable.user);
 errorDrawableId = a.getResourceId(
   R.styleable.lineEdittext_drawableError, R.drawable.user_error);
 a.recycle();
 init();
}

/**
 * 2014/7/31
 * 
 * @author Aimee.ZHANG
 */
private void init() {
 mPaint = new Paint();
 // mPaint.setStyle(Paint.Style.FILL);
 mPaint.setStrokeWidth(3.0f);
 color = Color.parseColor("#bfbfbf");
 setStatus(status);
 del_btn = mContext.getResources().getDrawable(R.drawable.del_but_bg);
 del_btn_down = mContext.getResources().getDrawable(R.drawable.del_but_bg_down);
 addListeners();
 setCompoundDrawablesWithIntrinsicBounds(left, null, null, null);
}

@Override
protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 mPaint.setColor(color);
 canvas.drawLine(0, this.getHeight() - 1, this.getWidth(),
   this.getHeight() - 1, mPaint);
}

// 刪除圖片
// private void setDrawable() { // if (length() < 1) { // setCompoundDrawablesWithIntrinsicBounds(left, null, null, null); // } else { // setCompoundDrawablesWithIntrinsicBounds(left, null, del_btn,null); // } // }

// 處理刪除事件
@Override
public boolean onTouchEvent(MotionEvent event) {
 if (del_btn != null && event.getAction() == MotionEvent.ACTION_UP) {
  // 獲取點擊時手指抬起的X坐標(biāo) 
  xUp = (int) event.getX(); 
  Log.e("xUp", xUp+""); 
  /*Rect rect = new Rect();
  getGlobalVisibleRect(rect);
  rect.left = rect.right - 50;*/
   // 當(dāng)點擊的坐標(biāo)到當(dāng)前輸入框右側(cè)的距離小于等于 getCompoundPaddingRight() 的距離時,則認(rèn)為是點擊了刪除圖標(biāo) 
  if ((getWidth() - xUp) <= getCompoundPaddingRight()) { 
   if (!TextUtils.isEmpty(getText().toString())) {
    setText(""); 
   } 
  }
 }else if(del_btn != null && event.getAction() == MotionEvent.ACTION_DOWN && getText().length()!=0){
  setCompoundDrawablesWithIntrinsicBounds(left,null,del_btn_down,null);
 }else if(getText().length()!=0){
  setCompoundDrawablesWithIntrinsicBounds(left,null,del_btn,null);
 }
 return super.onTouchEvent(event);
}

public void setStatus(int status) {
 this.status = status;


 if (status == STATUS_ERROR) {
  try {
   left = getResources().getDrawable(errorDrawableId);
  } catch (NotFoundException e) {
   e.printStackTrace();
  }
  setColor(Color.parseColor("#f57272"));
 } else if (status == STATUS_FOCUSED) {
  try {
   left = getResources().getDrawable(focusedDrawableId);
  } catch (NotFoundException e) {
   e.printStackTrace();
  }
  setColor(Color.parseColor("#5e99f3"));
 } else {
  try {
   left = getResources().getDrawable(unfocusedDrawableId);
  } catch (NotFoundException e) {
   e.printStackTrace();
  }
  setColor(Color.parseColor("#bfbfbf"));
 }
 if (left != null) {
// left.setBounds(0, 0, 30, 40); // this.setCompoundDrawables(left, null, null, null); setCompoundDrawablesWithIntrinsicBounds(left,null,null,null); } postInvalidate(); }

public void setLeftDrawable(int focusedDrawableId, int unfocusedDrawableId,
  int errorDrawableId) {
 this.focusedDrawableId = focusedDrawableId;
 this.unfocusedDrawableId = unfocusedDrawableId;
 this.errorDrawableId = errorDrawableId;
 setStatus(status);
}
 private void addListeners() { 
  try { 
   setOnFocusChangeListener(this); 
   addTextChangedListener(this); 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } 
 } 
@Override
protected void onFocusChanged(boolean focused, int direction,
  Rect previouslyFocusedRect) {
 super.onFocusChanged(focused, direction, previouslyFocusedRect);
 this.hasFocus=focused;
 if (focused) {
  setStatus(STATUS_FOCUSED);
 } else {
  setStatus(STATUS_UNFOCUSED);
  setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
 }
}

@Override
protected void finalize() throws Throwable {
 super.finalize();
};

public void setColor(int color) {
 this.color = color;
 this.setTextColor(color);
 invalidate();
}



@Override
public void afterTextChanged(Editable arg0) {
 // TODO Auto-generated method stub
 postInvalidate();
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
  int arg3) {
 // TODO Auto-generated method stub
  if (TextUtils.isEmpty(arg0)) { 
   // 如果為空,則不顯示刪除圖標(biāo) 
   setCompoundDrawablesWithIntrinsicBounds(left, null, null, null); 
  } else { 
   // 如果非空,則要顯示刪除圖標(biāo) 
   setCompoundDrawablesWithIntrinsicBounds(left, null, del_btn, null); 
  } 
}
@Override
 public void onTextChanged(CharSequence s, int start, int before, int after) { 
 if (hasFocus) { 
  if (TextUtils.isEmpty(s)) { 
   // 如果為空,則不顯示刪除圖標(biāo) 
   setCompoundDrawablesWithIntrinsicBounds(left, null, null, null); 
  } else { 
   // 如果非空,則要顯示刪除圖標(biāo) 
   setCompoundDrawablesWithIntrinsicBounds(left, null, del_btn, null); 
  } 
 } 
}

@Override
public void onFocusChange(View arg0, boolean arg1) {
 // TODO Auto-generated method stub
 try { 
  this.hasFocus = arg1; 
 } catch (Exception e) { 
  e.printStackTrace(); 
 } 
} 
}

比較關(guān)鍵的方法是:onTouchEvent

當(dāng)進(jìn)入界面,點擊輸入框,要判斷輸入框中是否已有文字,如果有則顯示灰色的刪除按鈕,如果沒有則不顯示,如果點擊了刪除按鈕,刪除按鈕變藍(lán)色

存在的問題: 這個版本依舊存在問題,就是輸入長度超過輸入框,所畫的線不會延伸,如圖


解決方法:

@Override

protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 mPaint.setColor(color);
 int x=this.getScrollX();
 int w=this.getMeasuredWidth();
 canvas.drawLine(0, this.getHeight() - 1, w+x,
   this.getHeight() - 1, mPaint);
}

w:獲取控件長度

X:延伸后的長度

最終效果:

以上就是Android實現(xiàn)自定義的EditText下劃線的方法,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • Android shell命令行中過濾adb logcat輸出的方法

    Android shell命令行中過濾adb logcat輸出的方法

    本文主要介紹Android shell命令行中過濾adb logcat輸出,這里詳細(xì)說明了shell 命令過濾logcat 輸出內(nèi)容,有需要的小伙伴可以參考下
    2016-08-08
  • 解析Android ANR問題

    解析Android ANR問題

    ANR 的全稱是 Application No Responding,即應(yīng)用程序無響應(yīng),具體是一些特定的 Message (Key Dispatch、Broadcast、Service) 在應(yīng)用的UI線程(主線程)沒有在規(guī)定的時間內(nèi)處理完,進(jìn)而觸發(fā) ANR 異常
    2021-01-01
  • Android設(shè)置圖片圓角的方法

    Android設(shè)置圖片圓角的方法

    這篇文章主要為大家詳細(xì)介紹了Android設(shè)置圖片圓角的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Android采集傳感器數(shù)據(jù)并顯示的方法

    Android采集傳感器數(shù)據(jù)并顯示的方法

    這篇文章主要為大家詳細(xì)介紹了Android采集傳感器數(shù)據(jù)并顯示的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Android自定義水波紋底部導(dǎo)航的實現(xiàn)

    Android自定義水波紋底部導(dǎo)航的實現(xiàn)

    TabLayout作為導(dǎo)航組件來說,使用場景非常的多,也意味著要滿足各種各樣的需求,這篇文章主要介紹了Android自定義水波紋底部導(dǎo)航的實現(xiàn)
    2022-08-08
  • Android 點擊ImageButton時有“按下”的效果的實現(xiàn)

    Android 點擊ImageButton時有“按下”的效果的實現(xiàn)

    這篇文章主要介紹了 Android 點擊ImageButton時有“按下”的效果的實現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Android使用Intent實現(xiàn)頁面跳轉(zhuǎn)

    Android使用Intent實現(xiàn)頁面跳轉(zhuǎn)

    這篇文章主要為大家詳細(xì)介紹了Android使用Intent實現(xiàn)頁面跳轉(zhuǎn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android中Progress的簡單實例

    Android中Progress的簡單實例

    這篇文章主要介紹了Android中Progress的簡單實例的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Android?數(shù)據(jù)壓縮淺析

    Android?數(shù)據(jù)壓縮淺析

    這篇文章主要為大家介紹了Android?數(shù)據(jù)壓縮方法淺析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Android自定義View實現(xiàn)可拖拽縮放的矩形框

    Android自定義View實現(xiàn)可拖拽縮放的矩形框

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)可拖拽縮放的矩形框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05

最新評論

绥化市| 岳普湖县| 祁门县| 郓城县| 张掖市| 恭城| 冷水江市| 东乡| 新沂市| 双鸭山市| 兴仁县| 胶州市| 共和县| 肃北| 北碚区| 南川市| 漠河县| 错那县| 西吉县| 治县。| 河池市| 丽水市| 乐东| 武鸣县| 讷河市| 洛扎县| 察哈| 宁南县| 赞皇县| 辽阳县| 门源| 远安县| 台安县| 洛阳市| 通州市| 盱眙县| 新闻| 卓尼县| 施秉县| 平乡县| 临沧市|