簡(jiǎn)單的驗(yàn)證碼

自定義控件

public class VerifyCodeViewextends RelativeLayout{

private EditTexteditText;

private TextView[]textViews;

private static int MAX =4;

private StringinputContent;

public VerifyCodeView(Context context) {

this(context,null);

}

public VerifyCodeView(Context context, AttributeSet attrs) {

this(context,attrs,0);

}

public VerifyCodeView(Context context, AttributeSet attrs,int defStyleAttr) {

super(context, attrs, defStyleAttr);

View.inflate(context, R.layout.view_verity_code,this);

textViews =new TextView[MAX];

textViews[0] =? findViewById(R.id.tv_0);

textViews[1] =? findViewById(R.id.tv_1);

textViews[2] =? findViewById(R.id.tv_2);

textViews[3] =? findViewById(R.id.tv_3);

editText =? findViewById(R.id.edit_text_view);

editText.setCursorVisible(false);//隱藏光標(biāo)

? ? ? ? setEditTextListener();

}

private void setEditTextListener() {

editText.addTextChangedListener(new TextWatcher() {

@Override

? ? ? ? ? ? public void beforeTextChanged(CharSequence charSequence,int i,int i1,int i2) {

}

@Override

? ? ? ? ? ? public void onTextChanged(CharSequence charSequence,int i,int i1,int i2) {

}

@Override

? ? ? ? ? ? public void afterTextChanged(Editable editable) {

inputContent =editText.getText().toString();

if (inputCompleteListener !=null) {

if (inputContent.length() >=MAX) {

inputCompleteListener.inputComplete();

}else {

inputCompleteListener.invalidContent();

}

}

for (int i =0; i

if (i

textViews[i].setText(String.valueOf(inputContent.charAt(i)));

}else {

textViews[i].setText("");

}

}

}

});

}

public VerifyCodeView(Context context, AttributeSet attrs,int defStyleAttr,int defStyleRes) {

super(context, attrs, defStyleAttr, defStyleRes);

}

private InputCompleteListenerinputCompleteListener;

public void setInputCompleteListener(InputCompleteListener inputCompleteListener) {

this.inputCompleteListener = inputCompleteListener;

}

public interface InputCompleteListener {

void inputComplete();

void invalidContent();

}

public String getEditContent() {

return inputContent;

}

}

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:layout_width="match_parent"

? ? android:layout_height="wrap_content">

? ? <LinearLayout

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="48dp"

? ? ? ? android:gravity="center"

? ? ? ? android:orientation="horizontal">

? ? ? ? <TextView

? ? ? ? ? ? android:id="@+id/tv_0"

? ? ? ? ? ? style="@style/textview_style"

? ? ? ? ? ? />

? ? ? ? <View

? ? ? ? ? ? android:layout_width="0dp"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_weight="1" />

? ? ? ? <TextView

? ? ? ? ? ? android:id="@+id/tv_1"

? ? ? ? ? ? style="@style/textview_style" />

? ? ? ? <View

? ? ? ? ? ? android:layout_width="0dp"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_weight="1" />

? ? ? ? <TextView

? ? ? ? ? ? android:id="@+id/tv_2"

? ? ? ? ? ? style="@style/textview_style" />

? ? ? ? <View

? ? ? ? ? ? android:layout_width="0dp"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_weight="1" />

? ? ? ? <TextView

? ? ? ? ? ? android:id="@+id/tv_3"

? ? ? ? ? ? style="@style/textview_style"

? ? ? ? ? ? />

? ? </LinearLayout>

? ? <LinearLayout

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="48dp"

? ? ? ? android:gravity="center"

? ? ? ? android:orientation="horizontal">

? ? ? ? <View

? ? ? ? ? ? android:layout_weight="1"

? ? ? ? ? ? android:layout_width="0dp"

? ? ? ? ? ? android:layout_height="2dp"

? ? ? ? ? ? android:layout_gravity="bottom"

? ? ? ? ? ? android:background="@color/color_white" />

? ? ? ? <View

? ? ? ? ? ? android:layout_width="0dp"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_weight="1" />

? ? ? ? <View

? ? ? ? ? ? android:layout_weight="1"

? ? ? ? ? ? android:layout_width="0dp"

? ? ? ? ? ? android:layout_height="2dp"

? ? ? ? ? ? android:layout_gravity="bottom"

? ? ? ? ? ? android:background="@color/color_white" />

? ? ? ? <View

? ? ? ? ? ? android:layout_width="0dp"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_weight="1" />

? ? ? ? <View

? ? ? ? ? ? android:layout_weight="1"

? ? ? ? ? ? android:layout_width="0dp"

? ? ? ? ? ? android:layout_height="2dp"

? ? ? ? ? ? android:layout_gravity="bottom"

? ? ? ? ? ? android:background="@color/color_white" />

? ? ? ? <View

? ? ? ? ? ? android:layout_width="0dp"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_weight="1" />

? ? ? ? <View

? ? ? ? ? ? android:layout_weight="1"

? ? ? ? ? ? android:layout_width="0dp"

? ? ? ? ? ? android:layout_height="2dp"

? ? ? ? ? ? android:layout_gravity="bottom"

? ? ? ? ? ? android:background="@color/color_white" />

? ? ? ? <View

? ? ? ? ? ? android:layout_width="0dp"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_weight="1" />

? ? ? ? <View

? ? ? ? ? ? android:layout_weight="1"

? ? ? ? ? ? android:layout_width="0dp"

? ? ? ? ? ? android:layout_height="2dp"

? ? ? ? ? ? android:layout_gravity="bottom"

? ? ? ? ? ? android:background="@color/color_white" />

? ? ? ? <View

? ? ? ? ? ? android:layout_width="0dp"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_weight="1" />

? ? ? ? <View

? ? ? ? ? ? android:layout_weight="1"

? ? ? ? ? ? android:layout_width="0dp"

? ? ? ? ? ? android:layout_height="2dp"

? ? ? ? ? ? android:layout_gravity="bottom"

? ? ? ? ? ? android:background="@color/color_white" />

? ? </LinearLayout>

? ? <MyEditText

? ? ? ? android:id="@+id/edit_text_view"

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="48dp"

? ? ? ? android:background="@android:color/transparent"

? ? ? ? android:inputType="number"

? ? ? ? android:longClickable="false"

? ? ? ? android:maxLength="4"

? ? ? ? android:textColor="@color/transparent" />

</RelativeLayout>


MyEditText

public class MyEditTextextends AppCompatEditText {

private long lastTime =0;

public MyEditText(Context context) {

super(context);

}

public MyEditText(Context context, AttributeSet attrs) {

super(context, attrs);

}

public MyEditText(Context context, AttributeSet attrs,int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

@Override

? ? protected void onSelectionChanged(int selStart,int selEnd) {

super.onSelectionChanged(selStart, selEnd);

this.setSelection(this.getText().length());

}

@Override

? ? public boolean onTouchEvent(MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

long currentTime = System.currentTimeMillis();

if (currentTime -lastTime <500) {

lastTime = currentTime;

return true;

}else {

lastTime = currentTime;

}

break;

}

return super.onTouchEvent(event);

}

}


Style

<style name="textview_style">

? ? ? ? <item name="android:layout_height">48dp</item>

? ? ? ? <item name="android:layout_width">0dp</item>

? ? ? ? <item name="android:background">@color/transparent</item>

? ? ? ? <item name="android:gravity">center</item>

? ? ? ? <item name="android:textColor">@color/black</item>

? ? ? ? <item name="android:textSize">15sp</item>

? ? ? ? <item name="android:textStyle">normal</item>

? ? </style>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末读慎,一起剝皮案震驚了整個(gè)濱河市壳炎,隨后出現(xiàn)的幾起案子码泞,更是在濱河造成了極大的恐慌铺然,老刑警劉巖迎吵,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異飞崖,居然都是意外死亡氮帐,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門扎阶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)汹胃,“玉大人婶芭,你說(shuō)我怎么就攤上這事⊥程ǎ” “怎么了雕擂?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)贱勃。 經(jīng)常有香客問(wèn)我井赌,道長(zhǎng),這世上最難降的妖魔是什么贵扰? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任仇穗,我火速辦了婚禮,結(jié)果婚禮上戚绕,老公的妹妹穿的比我還像新娘纹坐。我一直安慰自己,他們只是感情好舞丛,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布耘子。 她就那樣靜靜地躺著,像睡著了一般球切。 火紅的嫁衣襯著肌膚如雪谷誓。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,301評(píng)論 1 301
  • 那天吨凑,我揣著相機(jī)與錄音捍歪,去河邊找鬼。 笑死鸵钝,一個(gè)胖子當(dāng)著我的面吹牛糙臼,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播恩商,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼变逃,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了怠堪?” 一聲冷哼從身側(cè)響起韧献,我...
    開(kāi)封第一講書(shū)人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎研叫,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體璧针,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡嚷炉,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了探橱。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片申屹。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡绘证,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出哗讥,到底是詐尸還是另有隱情嚷那,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布杆煞,位于F島的核電站魏宽,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏决乎。R本人自食惡果不足惜队询,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望构诚。 院中可真熱鬧蚌斩,春花似錦、人聲如沸范嘱。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)丑蛤。三九已至叠聋,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間盏阶,已是汗流浹背晒奕。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留名斟,地道東北人脑慧。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像砰盐,于是被迫代替她去往敵國(guó)和親闷袒。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354