Android自定義EditText實(shí)現(xiàn)登錄界面
本文實(shí)例為大家分享了Android自定義EditText實(shí)現(xiàn)登錄界面的具體代碼,供大家參考,具體內(nèi)容如下
先看效果圖:

自定義edittext 控件,監(jiān)聽(tīng)focus和textchange 狀態(tài) 實(shí)現(xiàn)是否顯示刪除圖片。
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) {
// 這個(gè)屬性不加 沒(méi)法用
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加上圖片占據(jù)的寬度 在這個(gè)范圍內(nèi) 即判斷是否點(diǎn)擊了刪除按鈕
@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
}
/**
* 設(shè)置晃動(dòng)動(dòng)畫(huà)
*/
public void setShakeAnimation() {
this.setAnimation(shakeAnimation(5));
}
// 可以設(shè)置1秒鐘晃動(dòng)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 實(shí)現(xiàn)字體從上到下顯示:
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();
//轉(zhuǎn)換成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;
//轉(zhuǎn)化為 單個(gè)字的字符串
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;
}
/**返回字體最長(zhǎng)的寬度
* @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坐標(biāo)變化
float changeY = 0;
for (int j = 0; j < num; j++) {
//測(cè)量字體寬度和高度
measureText(strings[j]);
//沒(méi)個(gè)字體上下的間隔
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="帳號(hào)"
/>
<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="請(qǐng)輸入帳號(hào)"
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="請(qǐng)輸入密碼"
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="注冊(cè)"
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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android取消EditText自動(dòng)獲取焦點(diǎn)默認(rèn)行為
- Android控件系列之EditText使用方法
- android同時(shí)控制EditText輸入字符個(gè)數(shù)和禁止特殊字符輸入的方法
- Android中EditText實(shí)現(xiàn)不可編輯解決辦法
- Android定制自己的EditText輕松改變底線顏色
- Android中EditText顯示明文與密碼的兩種方式
- Android自定義EditText實(shí)現(xiàn)淘寶登錄功能
- Android EditText實(shí)現(xiàn)扁平化的登錄界面
- Android高級(jí)xml布局之輸入框EditText設(shè)計(jì)
- Android自定義控件EditText實(shí)現(xiàn)清除和抖動(dòng)功能
相關(guān)文章
Android程序開(kāi)發(fā)通過(guò)HttpURLConnection上傳文件到服務(wù)器
這篇文章主要介紹了Android程序開(kāi)發(fā)通過(guò)HttpURLConnection上傳文件到服務(wù)器的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android通過(guò)overScrollBy實(shí)現(xiàn)下拉視差特效
這篇文章主要為大家詳細(xì)介紹了Android通過(guò)overScrollBy實(shí)現(xiàn)下拉視差特效,實(shí)現(xiàn)精彩的阻尼效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android天氣預(yù)報(bào)之基于HttpGet對(duì)象解析天氣數(shù)據(jù)的方法
這篇文章主要介紹了Android天氣預(yù)報(bào)之基于HttpGet對(duì)象解析天氣數(shù)據(jù)的方法,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08
Android9?雙屏異顯實(shí)現(xiàn)方式思路
這篇文章主要為大家介紹了Android9?雙屏異顯實(shí)現(xiàn)方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
代碼分析Android實(shí)現(xiàn)側(cè)滑菜單
現(xiàn)在app越來(lái)越注重用戶體驗(yàn),本文給大家分析android實(shí)現(xiàn)側(cè)滑菜單的代碼,代碼簡(jiǎn)單易懂,感興趣的朋友一起看看吧2015-11-11
Android中關(guān)于Binder常見(jiàn)面試問(wèn)題小結(jié)
這篇文章主要介紹了Android中關(guān)于Binder幾個(gè)面試問(wèn)題,binder是一種進(jìn)程間通訊的機(jī)制,進(jìn)程間通訊需要了解用戶空間和內(nèi)核空間,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
android實(shí)現(xiàn)手機(jī)傳感器調(diào)用
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)手機(jī)傳感器調(diào)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
android異步任務(wù)設(shè)計(jì)思詳解(AsyncTask)
AsyncTask在Android十分常用,那為什么如此常用呢,不用行不行呢,內(nèi)部又是怎么實(shí)現(xiàn)的呢,為什么Java的API中沒(méi)有這個(gè)類(lèi)呢,看完本文后,你將會(huì)知道答案2014-02-02
android判斷phonegap是否聯(lián)網(wǎng)且加載super.loadUrl網(wǎng)址
android判斷phonegap是否聯(lián)網(wǎng)動(dòng)態(tài)加載super.loadUrl網(wǎng)址,接下來(lái)本文所提供的知識(shí)會(huì)幫助你解決以上問(wèn)題,感興趣的你可不要錯(cuò)過(guò)了哈2013-02-02

