Android自定义EditText实现登录界面

作者:JunTao_sun 时间:2022-07-03 11:59:25 

本文实例为大家分享了Android自定义EditText实现登录界面的具体代码,供大家参考,具体内容如下

先看效果图:

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, 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<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>

来源:http://blog.csdn.net/u013598111/article/details/50071647

标签:Android,EditText,登录界面
0
投稿

猜你喜欢

  • Android数据持久化之Preferences机制详解

    2021-09-04 23:42:32
  • 为什么阿里要慎重使用ArrayList中的subList方法

    2023-12-07 19:03:34
  • 深入了解Java对象的克隆

    2021-10-29 13:59:35
  • Java实现上传文件图片到指定服务器目录

    2023-06-28 00:23:32
  • Android模拟开关按钮点击打开动画(属性动画之平移动画)

    2021-09-05 17:48:52
  • java中staticclass静态类详解

    2021-10-12 19:47:35
  • Java 实现一个汉诺塔实战练习

    2021-12-20 14:49:47
  • Android View的事件分发机制深入分析讲解

    2023-06-18 21:28:49
  • C#读取XML的三种实现方式

    2023-02-08 10:07:01
  • SpringBoot如何使用自定义注解实现接口限流

    2023-11-25 07:22:37
  • Eclipse中改变默认的workspace的方法及说明详解

    2022-07-31 12:07:21
  • tk.mybatis实现uuid主键生成的示例代码

    2022-01-22 00:17:13
  • Android UI系列-----ScrollView和HorizontalScrollView的详解

    2022-04-06 14:14:08
  • C#创建、部署、调用WebService图文实例详解

    2022-05-03 06:35:20
  • JAVA字符串占位符使用方法实例

    2021-09-20 17:30:27
  • Java HashMap的工作原理

    2022-12-25 14:39:02
  • C#设计模式之ChainOfResponsibility职责链模式解决真假美猴王问题实例

    2023-04-01 00:39:00
  • Android调用摄像头拍照开发教程

    2023-05-24 11:33:33
  • 详谈jvm--Java中init和clinit的区别

    2022-01-10 10:35:22
  • Android百度地图应用开发基础知识

    2023-04-14 12:31:58
  • asp之家 软件编程 m.aspxhome.com