Android 自定义EditText( 登录界面)
来源:互联网 发布:网络出现问号 编辑:程序博客网 时间:2024/06/05 16:56
转载请标明出处:http://blog.csdn.net/u013598111/article/details/50071647,本文出自:【JunTao_sun】
先看效果图:
自定义edittext 控件,监听focus和textchange 状态 实现是否显示删除图片。
<span style="font-size:18px;">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);} @Overridepublic void onFocusChange(View v, boolean hasfocus) {this.hasfocus = hasfocus;if (hasfocus) {clearText(getText().length() > 0);} else {clearText(false);}}@Overridepublic void onTextChanged(CharSequence text, int start, int lengthBefore,int lengthAfter) {// TODO Auto-generated method stubif (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加上图片占据的宽度 在这个范围内 即判断是否点击了删除按钮@Overridepublic 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);}@Overridepublic void afterTextChanged(Editable arg0) {// TODO Auto-generated method stub}@Overridepublic 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;}}</span>自定义TextView 实现字体从上到下显示:
<span style="font-size:18px;">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();//转换成SPint 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);//////}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {// TODO Auto-generated method stubsuper.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<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;}@Overrideprotected 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);}}}</span>布局xml:
<span style="font-size:18px;"><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></span>button_selector xml
<span style="font-size:18px;"><?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></span>press:
<span style="font-size:18px;"><?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> </span>
normal:
<span style="font-size:18px;"><?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> </span>
0 0
- Android 自定义EditText( 登录界面)
- Android实战简易教程-第三十二枪(自定义View登录注册界面EditText-实现一键清空)
- Android实战简易教程-第三十二枪(自定义View登录注册界面EditText-实现一键清空)
- Android实战简易教程<三十二>(自定义View登录注册界面EditText-实现一键清空)
- Android自定义View-登录界面
- android 模仿qq登录界面EditText下拉框记住账号和密码 editText+popupwindow+sqlite方式实现(附源码)
- Android EditText——扁平化的登录界面
- Android 自定义 EditText(转)
- Android自定义控件-EditText(可用于登陆界面)
- 自定义Android的EditText实现仿淘宝登录功能
- 自定义BIEE登录界面
- springsecurity自定义登录界面
- 登录界面(Android studio)
- 登录界面异常(Android)
- Android-EditText(自定义带删除功能的EditText)
- Android-EditText(自定义带删除功能的EditText)
- android editText 自定义边框
- android 自定义edittext边框
- 插入Mysql时中文乱码解决方案
- IOS label导入带html标签文字,并且可以改变字体大小
- Android主题和样式之系统篇(上)
- Keil MDK C (error: #29:expected an expression)错误的解决
- Hibernate配置
- Android 自定义EditText( 登录界面)
- INI文件操作类
- java 生成excel根据jxl
- 排序——基本排序算法
- eclipse新建maven
- 四大域和九大隐式对象
- Android-类qq功能(三)
- js 数组和对象
- NGUI 的grid提高显示性能