First look at the effect chart
One: Layout code
The keyboard consists of 0~9 numbers, delete keys, and completion keys, or it can be GridView
defined by the adapter's method depending on the requirements getItemViewType
. Click on the key when the background has color effect.
The password input box consists of six EditText
components, each input box can enter a number, listen to the last input box to complete the password input end of the listening.
Two: keyboard
Key logical processing in keyboard, keyboard style, item
click event
@Override public
int Getviewtypecount () {return
2;
}
@Override public
int getitemviewtype (int position) {return
(getitemid (position) = Key_nine)? 2:1;
}
@Override public
View getview (int position, View Convertview, ViewGroup parent) {
Viewholder Viewholder = null;< C11/>if (Convertview = = null) {
if (getitemviewtype (position) = = 1) {
//numeric key
Convertview = Layoutinflater.from (context). Inflate (R.layout.item_grid_keyboard, parent, false);
Viewholder = new Viewholder (Convertview);
} else {
//delete key
Convertview = Layoutinflater.from (context). Inflate (R.layout.item_grid_keyboard_delete, Parent, false);
}
if (getitemviewtype (position) = = 1) {
Viewholder = (viewholder) convertview.gettag ();
ViewHolder.tvKey.setText (Key[position]);
return convertview;
}
Three: Password input box
Password input box to deal with the logic has to delete, input, input completion callback, etc.
/** * Enter the password, based on the number of data in StringBuilder logical judgment * @param value/public void Add (String value) {if Mpassword!= null & & Mpassword.length () < 6) {mpassword.append (value);//Add to StringBuilder first, and then display if in the input box (Mpassword.length ()
= = 1) {Tvfirst.settext (value);
else if (mpassword.length () = = 2) {tvsecond.settext (value);
}else if (mpassword.length () = = 3) {Tvthird.settext (value);
}else if (mpassword.length () = = 4) {Tvforth.settext (value);
}else if (mpassword.length () = = 5) {Tvfifth.settext (value);
}else if (mpassword.length () = = 6) {Tvsixth.settext (value); }}/** * Delete password, according to the number of data in StringBuilder logical judgement/public void remove () {if mpassword!= null && MPASSWORD.L
Ength () > 0) {if (mpassword.length () = = 1) {Tvfirst.settext ("");
else if (mpassword.length () = = 2) {Tvsecond.settext ("");
}else if (mpassword.length () = = 3) {Tvthird.settext (""); }else if (mPaSsword.length () = = 4) {Tvforth.settext ("");
}else if (mpassword.length () = = 5) {Tvfifth.settext ("");
}else if (mpassword.length () = = 6) {Tvsixth.settext ("");
} Mpassword.deletecharat (Mpassword.length ()-1); }
}
Get full password
/**
* Return full password
* @return
/Public String GetText () {returns
(Mpassword = null)? null:mPassword.toSt Ring ();
}
IV: Practical Application
Layout, you can also split the keyboard and slide it from the bottom of the window when you enter
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
android:layout_width=" match_parent "
android:layout_height=" match_parent ">
< Com.github.phoenix.widget.PayEditText
android:id= "@+id/payedittext_pay"
android:layout_width= "Match_ Parent "
android:layout_margintop=" 20DP "
android:paddingleft=" 12DP "
android:layout_alignparenttop=" True "
android:paddingright= 12dp"
android:layout_height= "48DP"/>
< Com.github.phoenix.widget.Keyboard
android:id= "@+id/keyboardview_pay"
android:layout_ Alignparentbottom= "true"
android:layout_width= "match_parent"
android:layout_height= "300DP"/>
</RelativeLayout>
In code
Key private static final string[] key = new string[] {"1", "2", "3", "4", "5", "6", "7", "8", "9",
"<<", "0", "complete"};
Set keyboard Keyboard.setkeyboardkeys (key); Keyboard Key Click event Keyboard.setonclickkeyboardlistener (New Keyboard.onclickkeyboardlistener () {@Override public void Onkeycl
ick (int position, String value) {if (Position < && position!= 9) {Payedittext.add (value);
else if (position = 9) {payedittext.remove (); }else if (position = = 11) {//when clicking the completion button on the keyboard, you can also obtain the password via Payedittext.gettext (), you should not register the Oninputfinishedlistener interface Toa
St.maketext (Getapplication (), "Your password is:" + payedittext.gettext (), Toast.length_short). Show ();
Finish ();
}
}
}); Callback Payedittext.setoninputfinishedlistener when password input is complete (new Payedittext.oninputfinishedlistener () {@Override public void oninputfinished (String password) {toast.maketext (Getapplication (), "Your password is:" + password, toast.length_short). Show
(); }
});
Summarize
OK, the above is the entire content of this article, I hope this article on the content of Android developers can help, if you have questions you can message exchange.