
本文实例为大家分享了AndroID自定义EditText实现登录界面的具体代码,供大家参考,具体内容如下
先看效果图:
自定义edittext 控件,监听focus和textchange 状态 实现是否显示删除图片。
public class ClearEditText extends EditText implements OnFocuschangelistener,TextWatcher { private Drawable right; private boolean hasfocus; private Drawable mClearDrawable; public ClearEditText(Context context) { this(context,null); } public ClearEditText(Context context,AttributeSet attrs) { // 这个属性不加 没法用 this(context,attrs,androID.R.attr.editTextStyle); } public ClearEditText(Context context,AttributeSet attrs,int defStyle) { super(context,defStyle); // 初始化删除的资源图片 mClearDrawable = getCompoundDrawables()[2]; if (mClearDrawable == null) { mClearDrawable = getResources().getDrawable(R.drawable.ic_close1); } mClearDrawable.setBounds(0,mClearDrawable.getIntrinsicWIDth(),mClearDrawable.getIntrinsicHeight()); clearText(false); setonFocuschangelistener(this); addTextChangedListener(this); } @OverrIDe public voID onFocusChange(VIEw v,boolean hasfocus) { this.hasfocus = hasfocus; if (hasfocus) { clearText(getText().length() > 0); } else { clearText(false); } } @OverrIDe public voID onTextChanged(CharSequence text,int start,int lengthBefore,int lengthAfter) { // Todo auto-generated method stub if (hasfocus) { clearText(text.length() > 0); } } private voID clearText(boolean visible) { if (visible) { right = mClearDrawable; } else { right = null; } setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1],right,getCompoundDrawables()[3]); // right.setBounds(0,right.getIntrinsicWIDth(),// right.getIntrinsicHeight()); } //getTotalpaddingRight 返回 又padding加上图片占据的宽度 在这个范围内 即判断是否点击了删除按钮 @OverrIDe public boolean ontouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { if (getCompoundDrawables()[2] != null) { boolean t = event.getX() > (getWIDth() - getTotalpaddingRight()) && (event.getX() < ((getWIDth() - getpaddingRight()))); if (t) { this.setText(""); } } } return super.ontouchEvent(event); } @OverrIDe public voID afterTextChanged(Editable arg0) { // Todo auto-generated method stub } @OverrIDe public voID beforeTextChanged(CharSequence arg0,int arg1,int arg2,int arg3) { // Todo auto-generated method stub } /** * 设置晃动动画 */ public voID setShakeAnimation() { this.setAnimation(shakeAnimation(5)); } // 可以设置1秒钟晃动s下 public static Animation shakeAnimation(int s) { Animation translateAnimation = new TranslateAnimation(0,10,0); translateAnimation.setInterpolator(new CycleInterpolator(s)); translateAnimation.setDuration(1000); return translateAnimation; } } 自定义TextVIEw 实现字体从上到下显示:
public class CustomText extends TextVIEw { private String text; private Paint paint; private Rect rect = new Rect(); private int inittopdistance = 8; public CustomText(Context context) { super(context,null); // Todo auto-generated constructor stub } public CustomText(Context context,AttributeSet attrs) { this(context,0); // Todo auto-generated constructor stub } public CustomText(Context context,defStyle); text = (String) getText(); displayMetrics metric = new displayMetrics(); WindowManager windowmanager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); windowmanager.getDefaultdisplay().getMetrics(metric); //得到字体大小 int size = (int) getTextSize(); //转换成SP int s= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,size,metric); paint = new Paint(Paint.ANTI_AliAS_FLAG); paint.setDither(true); paint.setcolor(0xffffffff); if(s!=0) paint.setTextSize(s); Typeface t= Typeface.createFromAsset(context.getResources().getAssets(),"Fonts/Font.TTF"); paint.setTypeface(t); paint.setShadowLayer(60,30,0xff00ffff); } // @OverrIDe // protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { // //// super.onMeasure(wIDthMeasureSpec,heightmeasureSpec); // int modeWIDth=MeasureSpec.getMode(wIDthMeasureSpec); // int modeHeight=MeasureSpec.getMode(heightmeasureSpec); // int wIDthSize=MeasureSpec.getSize(wIDthMeasureSpec); // int heightSize=MeasureSpec.getSize(heightmeasureSpec); // // int wIDth=0; // int heigh=0; // if(modeWIDth==MeasureSpec.AT_MOST) // // wIDth=getMaxTextWdith(getStrings())+getpaddingleft()+getpaddingRight(); // // if(modeHeight==MeasureSpec.AT_MOST) // heigh=getMaxTextHeight(getStrings())+getpaddingtop()+getpaddingBottom(); // // setMeasuredDimension(wIDth=modeWIDth==MeasureSpec.AT_MOST?wIDth:wIDthSize,// height=modeHeight==MeasureSpec.AT_MOST?height:heightSize); // // // } @OverrIDe protected voID onSizeChanged(int w,int h,int olDW,int oldh) { // Todo auto-generated method stub super.onSizeChanged(w,h,olDW,oldh); wIDth = w; height = h; } private voID measureText(String str) { paint.getTextBounds(str,str.length(),rect); FontMetrics fm = paint.getFontMetrics(); textHeight = (int) (fm.ascent + fm.descent); } private int textHeight; private int wIDth; private int height; private int num; //转化为 单个字的字符串 public String[] getStrings(){ num = text.length(); String[] strings = new String[num]; for (int i = 0; i < num; i++) { char c = text.charat(i); strings[i] = String.valueOf(c); } return strings; } /**返回字体最长的宽度 * @param strs * @return */ public int getMaxTextWdith(String[] strs){ int w=0; for(int i=0;i<strs.length;i++){ measureText(strs[i]); w=Math.max(rect.wIDth(),w); } return w; } /**返回字体最高的高度 * @param strs * @return */ public int getMaxTextHeight(String[] strs){ int h=0; for(int i=0;i<strs.length;i++){ measureText(strs[i]); h=Math.max(-textHeight,h); } return h; } @OverrIDe protected voID onDraw(Canvas canvas) { String[] strings=getStrings(); float starty = 1.0f * height / num; //Y坐标变化 float changeY = 0; for (int j = 0; j < num; j++) { //测量字体宽度和高度 measureText(strings[j]); //没个字体上下的间隔 changeY = starty * j; int left=getWIDth() / 2 - rect.wIDth() / 2 + getpaddingleft() + getpaddingRight(); int top=(int) (starty/2-textHeight+ changeY + getpaddingtop() + getpaddingBottom()); canvas.drawText(strings[j],left,top,paint); } } } 布局xml:
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" > <relativeLayout androID:ID="@+ID/meishi" androID:layout_wIDth="match_parent" androID:layout_height="0dp" androID:layout_weight="1" androID:background="@drawable/ic_meishi" > <com.example.eIDttext.CustomText androID:ID="@+ID/tttt" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:layout_alignParentRight="true" androID:text="味道" androID:textSize="40sp" /> </relativeLayout> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="0dp" androID:layout_weight="1" androID:orIEntation="vertical" > <relativeLayout androID:ID="@+ID/count" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:padding="8dp" androID:gravity="center" androID:layout_margintop="20dp" > <TextVIEw androID:ID="@+ID/text_count" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="8dp" androID:text="帐号" /> <com.example.eIDttext.ClearEditText androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_marginRight="20dp" androID:layout_toRightOf="@+ID/text_count" androID:background="@drawable/edittext" androID:drawableRight="@drawable/ic_close1" androID:gravity="center_vertical" androID:hint="请输入帐号" androID:textSize="16sp" androID:padding="8dp" androID:singleline="true" > </com.example.eIDttext.ClearEditText> </relativeLayout> <relativeLayout androID:ID="@+ID/password" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:padding="8dp" > <TextVIEw androID:ID="@+ID/text_password" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="8dp" androID:text="密码" /> <com.example.eIDttext.ClearEditText androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_marginRight="20dp" androID:layout_toRightOf="@+ID/text_password" androID:background="@drawable/edittext" androID:drawableRight="@drawable/ic_close1" androID:gravity="center_vertical" androID:hint="请输入密码" androID:padding="8dp" androID:textSize="16sp" androID:singleline="true" > </com.example.eIDttext.ClearEditText> </relativeLayout> <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:padding="16dp" > <button androID:ID="@+ID/login" androID:layout_wIDth="wrap_content" androID:layout_height="30dp" androID:layout_marginleft="50dp" androID:textSize="16sp" androID:background="@drawable/button_selector" androID:text="登录" /> <button androID:layout_wIDth="wrap_content" androID:layout_height="30dp" androID:layout_marginleft="80dp" androID:layout_toRightOf="@+ID/login" androID:background="@drawable/button_selector" androID:text="注册" androID:textSize="16sp" /> </relativeLayout> </linearLayout> </linearLayout>
button_selector xml
<?xml version="1.0" enCoding="utf-8"?> <selector xmlns:androID="http://schemas.androID.com/apk/res/androID" > <item androID:state_pressed="true" androID:drawable="@drawable/button_press" />" <item androID:drawable="@drawable/button" /> </selector>
press:
<?xml version="1.0" enCoding="utf-8"?> <shape androID:shape="rectangle" xmlns:androID="http://schemas.androID.com/apk/res/androID"> <gradIEnt androID:startcolor="@color/deep_red" androID:centercolor="#ffffffff" androID:endcolor="@color/oranger_red" androID:angle="90" > </gradIEnt> <corners androID:radius="15dp" /> <stroke androID:wIDth="1px" androID:color="#FF6666"/> </shape>
normal:
<?xml version="1.0" enCoding="utf-8"?> <shape androID:shape="rectangle" xmlns:androID="http://schemas.androID.com/apk/res/androID"> <gradIEnt androID:startcolor="#FF3333" androID:centercolor="#ffffffff" androID:endcolor="#FF9966" androID:angle="90" > </gradIEnt> <corners androID:radius="15dp" /> <stroke androID:wIDth="1px" androID:color="#ededed"/> </shape>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android自定义EditText实现登录界面全部内容,希望文章能够帮你解决Android自定义EditText实现登录界面所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)