Android high imitation WeChat payment digital keyboard function

Source: Internet
Author: User
Currently, many apps use custom keyboards for payment and password input, which is convenient and practical. The following article introduces the Android high-imitation payment digital keyboard function, which is very good. if you are interested, let's take a look at it. now, many apps use custom keyboards for payment and password input, convenient and practical. The following article brings you the http://www.php.cn/wiki/1502.html "target =" _ blank "> Android high imitation payment digital keyboard function, very good, interested friends to learn it together

Next we will take you to learn how to use a high-imitation digital keyboard for your own projects.

Let's take a look at it first:

1. Custom layout

 
 
  
  
  
  
   
  
 
 

The keyboard layout is essentially a 4x3 grid layout of the GridView.

2. implement the keyboard content

Import android. content. context; import android. util. attributeSet; import android. view. view; import android. widget. gridView; import android. widget. relativeLayout; import com. lnyp. pswkeyboard. r; import com. lnyp. pswkeyboard. adapter. keyBoardAdapter; import java. util. arrayList; import java. util. hashMap; import java. util. map;/*** virtual keyboard */public class VirtualKeyboardView extends RelativeLayout implements View. onClickListener {Context context; private GridView; private RelativeLayout layoutBack; private ArrayList
 
  
> ValueList; public VirtualKeyboardView (Context context) {this (context, null);} public VirtualKeyboardView (Context context, AttributeSet attrs) {super (context, attrs); this. context = context; View view = View. inflate (context, R. layout. layout_virtual_keyboard, null); valueList = new ArrayList <> (); layoutBack = (RelativeLayout) view. findViewById (R. id. layoutBack); layoutBack. setOnClickListener (this); gridView = (GridView) view. findViewById (R. id. gv_keybord); setView (); addView (view);} public RelativeLayout getLayoutBack () {return layoutBack;} public ArrayList
  
   
> GetValueList () {return valueList;} public GridView getGridView () {return gridView;} private void setView () {/* number to be displayed on the initialization button */for (int I = 1; I <13; I ++) {Map
   
    
Map = new HashMap
    
     
(); If (I <10) {map. put ("name", String. valueOf (I);} else if (I = 10) {map. put ("name ",". ");} else if (I = 11) {map. put ("name", String. valueOf (0);} else if (I = 12) {map. put ("name", "");} valueList. add (map);} KeyBoardAdapter keyBoardAdapter = new KeyBoardAdapter (context, valueList); gridView. setAdapter (keyBoardAdapter) ;}@ Overridepublic void onClick (View v ){}}
    
   
  
 

See how the adapter works: KeyBoardAdapter. java

Import android. content. context; import android. graphics. color; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. relativeLayout; import android. widget. textView; import com. lnyp. pswkeyboard. r; import java. util. arrayList; import java. util. map;/*** keyboard adapter */public class KeyBoardAdapter extends BaseAdapter {private Context mContext; private ArrayList
 
  
> ValueList; public KeyBoardAdapter (Context mContext, ArrayList
  
   
> ValueList) {this. mContext = mContext; this. valueList = valueList;} @ Overridepublic int getCount () {return valueList. size () ;}@ Overridepublic Object getItem (int position) {return valueList. get (position) ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {ViewHolder viewHolder; if (convertView = null) {convertView = View. inflate (mContext, R. layout. grid_item_virtual_keyboard, null); viewHolder = new ViewHolder (); viewHolder. btnKey = (TextView) convertView. findViewById (R. id. btn_keys); viewHolder. imgDelete = (RelativeLayout) convertView. findViewById (R. id. imgDelete); convertView. setTag (viewHolder);} else {viewHolder = (ViewHolder) convertView. getTag () ;}if (position = 9) {viewHolder. imgDelete. setVisibility (View. INVISIBLE); viewHolder. btnKey. setVisibility (View. VISIBLE); viewHolder. btnKey. setText (valueList. get (position ). get ("name"); viewHolder. btnKey. setBackgroundColor (Color. parseColor ("# e0e0e0");} else if (position = 11) {viewHolder. btnKey. setBackgroundResource (R. mipmap. keyboard_delete_img); viewHolder. imgDelete. setVisibility (View. VISIBLE); viewHolder. btnKey. setVisibility (View. INVISIBLE);} else {viewHolder. imgDelete. setVisibility (View. INVISIBLE); viewHolder. btnKey. setVisibility (View. VISIBLE); viewHolder. btnKey. setText (valueList. get (position ). get ("name");} return convertView;}/*** Storage Control */public final class ViewHolder {public TextView btnKey; public RelativeLayout imgDelete ;}}
  
 

Before reading the Adapter, let's first look at how grid_item_virtual_keyboard is implemented:

 
 
  
  
   
  
 

As you can see, two views are specified in the item layout file. one is the TextView for displaying ordinary numbers and the other is the RelativeLayout for displaying the last deletion key.
Then, in the getView method of the KeyBoardAdapter, we perform different la s based on the position. When position is 9, that is, the last and third buttons, the button color must be set separately. When position is 12, that is, the last button, you need to control the display of the delete button and hide the numeric button. In other cases, the delete button is hidden and the numeric button is displayed.

3. use and implement the keyboard event logic

In the layout, you can directly use your own digital keyboard:

 
 
  
  
 

In the Activity, we operate the numeric keyboard:

Import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. text. editable; import android. view. view; import android. view. animation. animation; import android. view. animation. animationUtils; import android. widget. adapterView; import android. widget. editText; import android. widget. gridView; import com. lnyp. pswkeyboard. widget. virtualKeyboardView; import java. util. arrayList; import java. util. map; public class NormalKeyBoardActivity extends AppCompatActivity {private VirtualKeyboardView virtualKeyboardView; private GridView; private ArrayList
 
  
> ValueList; private EditText textAmount; private Animation enterAnim; private Animation exitAnim; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_normal_key_board); valueList = virtualKeyboardView. getValueList (); initAnim (); initView ();} private void initAnim () {enterAnim = AnimationUtils. loadAnimation (this, R. anim. push_bottom_in); exitAnim = AnimationUtils. loadAnimation (this, R. anim. push_bottom_out);} private void initView () {virtualKeyboardView = (VirtualKeyboardView) findViewById (R. id. virtualKeyboardView); textAmount = (EditText) findViewById (R. id. textAmount); virtualKeyboardView. getLayoutBack (). setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {virtualKeyboardView. startAnimation (exitAnim); virtualKeyboardView. setVisibility (View. GONE) ;}}); gridView = virtualKeyboardView. getGridView (); gridView. setOnItemClickListener (onItemClickListener); textAmount. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {virtualKeyboardView. setFocusable (true); virtualKeyboardView. setFocusableInTouchMode (true); virtualKeyboardView. startAnimation (enterAnim); virtualKeyboardView. setVisibility (View. VISIBLE) ;}}) ;}private AdapterView. onItemClickListener onItemClickListener = new AdapterView. onItemClickListener () {@ Overridepublic void onItemClick (AdapterView
  AdapterView, View view, int position, long l) {if (position <11 & position! = 9) {// Click 0 ~ 9 Button String amount = textAmount. getText (). toString (). trim (); amount = amount + valueList. get (position ). get ("name"); textAmount. setText (amount); Editable ea = textAmount. getText (); textAmount. setSelection (ea. length ();} else {if (position = 9) {// Click the return key String amount = textAmount. getText (). toString (). trim (); if (! Amount. contains (". ") {amount = amount + valueList. get (position ). get ("name"); textAmount. setText (amount); Editable ea = textAmount. getText (); textAmount. setSelection (ea. length () ;}}if (position = 11) {// Click the return key String amount = textAmount. getText (). toString (). trim (); if (amount. length ()> 0) {amount = amount. substring (0, amount. length ()-1); textAmount. setText (amount); Editable ea = textAmount. getText (); textAmount. setSelection (ea. length ());}}}}};}
 

The above is a detailed description of the Android high-imitation payment digital keyboard function. For more information, see other related articles on php Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.