現在很多app的付款、輸入密碼功能,都是使用自訂數字鍵盤,方便實用。以下本文為大家帶來了Android 高仿微信支付數字鍵盤功能,非常不錯,有興趣的朋友一起學習吧
下面帶著大家學習下,如何高仿微信的數字鍵盤,可以拿來直接用在自身的專案中。下效果圖:
#1. 自訂佈局
<?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="wrap_content"> <!-- 输入键盘 --> <GridView android:id="@+id/gv_keybord" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#bdbdbd" android:horizontalSpacing="1px" android:numColumns="3" android:verticalSpacing="1px" /> <View android:id="@+id/line" android:layout_width="match_parent" android:layout_height="1px" android:layout_above="@id/gv_keybord" android:background="#bdbdbd" /> <RelativeLayout android:id="@+id/layoutBack" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/line" android:background="#f5f5f5" android:padding="10dp"> <ImageView android:id="@+id/imgBack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@mipmap/keyboard_back_img" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="1px" android:layout_above="@id/layoutBack" android:layout_marginTop="1dp" android:background="#bdbdbd" /> </RelativeLayout>
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; /** * 虚拟键盘 */ public class VirtualKeyboardView extends RelativeLayout implements View.OnClickListener { Context context; private GridView gridView; private RelativeLayout layoutBack; private ArrayList<Map<String, String>> 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<Map<String, String>> getValueList() { return valueList; } public GridView getGridView() { return gridView; } private void setView() { /* 初始化按钮上应该显示的数字 */ for (int i = 1; i < 13; i++) { Map<String, String> map = new HashMap<String, String>(); 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); } @Override public void onClick(View v) { } }
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; /** * 九宫格键盘适配器 */ public class KeyBoardAdapter extends BaseAdapter { private Context mContext; private ArrayList<Map<String, String>> valueList; public KeyBoardAdapter(Context mContext, ArrayList<Map<String, String>> valueList) { this.mContext = mContext; this.valueList = valueList; } @Override public int getCount() { return valueList.size(); } @Override public Object getItem(int position) { return valueList.get(position); } @Override public long getItemId(int position) { return position; } @Override public 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; } /** * 存放控件 */ public final class ViewHolder { public TextView btnKey; public RelativeLayout imgDelete; } }
<?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" android:background="#e0e0e0"> <TextView android:id="@+id/btn_keys" android:layout_width="match_parent" android:layout_height="60dp" android:layout_centerInParent="true" android:background="@drawable/selector_gird_item" android:gravity="center" android:includeFontPadding="false" android:textColor="#333333" android:textSize="26sp" /> <RelativeLayout android:id="@+id/imgDelete" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_centerInParent="true"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@mipmap/keyboard_delete_img" /> </RelativeLayout> </RelativeLayout>
以上是Android 高仿微信支付數位鍵盤功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!