Android中怎么通过自定义EditText实现登录界面
Android中怎么通过自定义EditText实现登录界面,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
创新互联于2013年创立,先为解放等服务建站,解放等地企业,进行企业商务咨询服务。为解放企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
自定义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, attrs, defStyle); // 初始化删除的资源图片 mClearDrawable = getCompoundDrawables()[2]; if (mClearDrawable == null) { mClearDrawable = getResources().getDrawable(R.drawable.ic_close1); } mClearDrawable.setBounds(0, 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, 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, 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, attrs, 0); // TODO Auto-generated constructor stub } public CustomText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, 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, 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, 0, 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布局xml:
button_selector xml
- "
press:
normal:
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
网站名称:Android中怎么通过自定义EditText实现登录界面
本文链接:http://myzitong.com/article/jidcsg.html