├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── libraries │ ├── animated_vector_drawable_24_1_1.xml │ ├── appcompat_v7_24_1_1.xml │ ├── hamcrest_core_1_3.xml │ ├── junit_4_12.xml │ ├── support_annotations_24_1_1.xml │ ├── support_v4_24_1_1.xml │ └── support_vector_drawable_24_1_1.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── lnyp │ │ └── pswkeyboard │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── lnyp │ │ │ └── pswkeyboard │ │ │ ├── MainActivity.java │ │ │ ├── NormalKeyBoardActivity.java │ │ │ ├── OnPasswordInputFinish.java │ │ │ ├── PaymentKeyBoardActivity.java │ │ │ ├── adapter │ │ │ └── KeyBoardAdapter.java │ │ │ └── widget │ │ │ ├── PasswordView.java │ │ │ ├── PopEnterPassword.java │ │ │ └── VirtualKeyboardView.java │ └── res │ │ ├── anim │ │ ├── push_bottom_in.xml │ │ └── push_bottom_out.xml │ │ ├── drawable │ │ ├── img_psw_bg.xml │ │ ├── psw_input_area_bg.xml │ │ └── selector_gird_item.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── activity_normal_key_board.xml │ │ ├── activity_payment_key_board.xml │ │ ├── grid_item_virtual_keyboard.xml │ │ ├── layout_popup_bottom.xml │ │ ├── layout_virtual_keyboard.xml │ │ └── pop_enter_password.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ ├── ahn.png │ │ ├── default_nor_avatar.png │ │ ├── ic_launcher.png │ │ ├── keyboard_back_img.png │ │ └── keyboard_delete_img.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── lnyp │ └── pswkeyboard │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── imgs └── img.gif └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/workspace.xml 38 | 39 | # Keystore files 40 | *.jks 41 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | WeChatPswKeyboard -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 23 | -------------------------------------------------------------------------------- /.idea/libraries/animated_vector_drawable_24_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/libraries/appcompat_v7_24_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /.idea/libraries/hamcrest_core_1_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/junit_4_12.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/support_annotations_24_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/support_v4_24_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /.idea/libraries/support_vector_drawable_24_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WeChatPswKeyboard 2 | 高仿微信数字键盘、支付键盘(密码键盘) 3 | 4 | 效果图: 5 | 6 | ![image](https://github.com/zuiwuyuan/WeChatPswKeyboard/blob/master/imgs/img.gif) 7 | 8 | 详细介绍,请查看:http://blog.csdn.net/zuiwuyuan/article/details/52198240 9 | 10 | 如果对你有帮助,请star下吧,谢谢。 11 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion "24.0.1" 6 | 7 | defaultConfig { 8 | applicationId "com.lnyp.pswkeyboard" 9 | minSdkVersion 15 10 | targetSdkVersion 24 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:24.1.1' 26 | } 27 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Java\android-sdk-windows/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/lnyp/pswkeyboard/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.lnyp.pswkeyboard; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/lnyp/pswkeyboard/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.lnyp.pswkeyboard; 2 | 3 | import android.content.Intent; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.os.Bundle; 6 | import android.view.View; 7 | 8 | public class MainActivity extends AppCompatActivity { 9 | 10 | @Override 11 | protected void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | setContentView(R.layout.activity_main); 14 | } 15 | 16 | public void toNormalKeyBoard(View view) { 17 | startActivity(new Intent(this, NormalKeyBoardActivity.class)); 18 | } 19 | 20 | public void toPayKeyBoard(View view) { 21 | startActivity(new Intent(this, PaymentKeyBoardActivity.class)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/lnyp/pswkeyboard/NormalKeyBoardActivity.java: -------------------------------------------------------------------------------- 1 | package com.lnyp.pswkeyboard; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.text.Editable; 6 | import android.text.InputType; 7 | import android.view.View; 8 | import android.view.WindowManager; 9 | import android.view.animation.Animation; 10 | import android.view.animation.AnimationUtils; 11 | import android.widget.AdapterView; 12 | import android.widget.EditText; 13 | import android.widget.GridView; 14 | 15 | import com.lnyp.pswkeyboard.widget.VirtualKeyboardView; 16 | 17 | import java.lang.reflect.Method; 18 | import java.util.ArrayList; 19 | import java.util.Map; 20 | 21 | public class NormalKeyBoardActivity extends AppCompatActivity { 22 | 23 | private VirtualKeyboardView virtualKeyboardView; 24 | 25 | private GridView gridView; 26 | 27 | private ArrayList> valueList; 28 | 29 | private EditText textAmount; 30 | 31 | private Animation enterAnim; 32 | 33 | private Animation exitAnim; 34 | 35 | @Override 36 | protected void onCreate(Bundle savedInstanceState) { 37 | super.onCreate(savedInstanceState); 38 | setContentView(R.layout.activity_normal_key_board); 39 | 40 | initAnim(); 41 | 42 | initView(); 43 | 44 | valueList = virtualKeyboardView.getValueList(); 45 | } 46 | 47 | /** 48 | * 数字键盘显示动画 49 | */ 50 | private void initAnim() { 51 | 52 | enterAnim = AnimationUtils.loadAnimation(this, R.anim.push_bottom_in); 53 | exitAnim = AnimationUtils.loadAnimation(this, R.anim.push_bottom_out); 54 | } 55 | 56 | private void initView() { 57 | 58 | textAmount = (EditText) findViewById(R.id.textAmount); 59 | 60 | // 设置不调用系统键盘 61 | if (android.os.Build.VERSION.SDK_INT <= 10) { 62 | textAmount.setInputType(InputType.TYPE_NULL); 63 | } else { 64 | this.getWindow().setSoftInputMode( 65 | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 66 | try { 67 | Class cls = EditText.class; 68 | Method setShowSoftInputOnFocus; 69 | setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", 70 | boolean.class); 71 | setShowSoftInputOnFocus.setAccessible(true); 72 | setShowSoftInputOnFocus.invoke(textAmount, false); 73 | } catch (Exception e) { 74 | e.printStackTrace(); 75 | } 76 | } 77 | 78 | virtualKeyboardView = (VirtualKeyboardView) findViewById(R.id.virtualKeyboardView); 79 | virtualKeyboardView.getLayoutBack().setOnClickListener(new View.OnClickListener() { 80 | @Override 81 | public void onClick(View v) { 82 | virtualKeyboardView.startAnimation(exitAnim); 83 | virtualKeyboardView.setVisibility(View.GONE); 84 | } 85 | }); 86 | 87 | gridView = virtualKeyboardView.getGridView(); 88 | gridView.setOnItemClickListener(onItemClickListener); 89 | 90 | textAmount.setOnClickListener(new View.OnClickListener() { 91 | @Override 92 | public void onClick(View v) { 93 | 94 | virtualKeyboardView.setFocusable(true); 95 | virtualKeyboardView.setFocusableInTouchMode(true); 96 | 97 | virtualKeyboardView.startAnimation(enterAnim); 98 | virtualKeyboardView.setVisibility(View.VISIBLE); 99 | } 100 | }); 101 | 102 | } 103 | 104 | private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() { 105 | 106 | @Override 107 | public void onItemClick(AdapterView adapterView, View view, int position, long l) { 108 | 109 | if (position < 11 && position != 9) { //点击0~9按钮 110 | 111 | String amount = textAmount.getText().toString().trim(); 112 | amount = amount + valueList.get(position).get("name"); 113 | 114 | textAmount.setText(amount); 115 | 116 | Editable ea = textAmount.getText(); 117 | textAmount.setSelection(ea.length()); 118 | } else { 119 | 120 | if (position == 9) { //点击退格键 121 | String amount = textAmount.getText().toString().trim(); 122 | if (!amount.contains(".")) { 123 | amount = amount + valueList.get(position).get("name"); 124 | textAmount.setText(amount); 125 | 126 | Editable ea = textAmount.getText(); 127 | textAmount.setSelection(ea.length()); 128 | } 129 | } 130 | 131 | if (position == 11) { //点击退格键 132 | String amount = textAmount.getText().toString().trim(); 133 | if (amount.length() > 0) { 134 | amount = amount.substring(0, amount.length() - 1); 135 | textAmount.setText(amount); 136 | 137 | Editable ea = textAmount.getText(); 138 | textAmount.setSelection(ea.length()); 139 | } 140 | } 141 | } 142 | } 143 | }; 144 | } 145 | -------------------------------------------------------------------------------- /app/src/main/java/com/lnyp/pswkeyboard/OnPasswordInputFinish.java: -------------------------------------------------------------------------------- 1 | package com.lnyp.pswkeyboard; 2 | 3 | /** 4 | * 自定义接口,用于给密码输入完成添加回掉事件 5 | */ 6 | public interface OnPasswordInputFinish { 7 | void inputFinish(String password); 8 | } -------------------------------------------------------------------------------- /app/src/main/java/com/lnyp/pswkeyboard/PaymentKeyBoardActivity.java: -------------------------------------------------------------------------------- 1 | package com.lnyp.pswkeyboard; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.view.Gravity; 6 | import android.view.View; 7 | 8 | import com.lnyp.pswkeyboard.widget.PopEnterPassword; 9 | 10 | public class PaymentKeyBoardActivity extends AppCompatActivity { 11 | 12 | @Override 13 | protected void onCreate(Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | setContentView(R.layout.activity_payment_key_board); 16 | } 17 | 18 | public void showPayKeyBoard(View view) { 19 | 20 | PopEnterPassword popEnterPassword = new PopEnterPassword(this); 21 | 22 | // 显示窗口 23 | popEnterPassword.showAtLocation(this.findViewById(R.id.layoutContent), 24 | Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/lnyp/pswkeyboard/adapter/KeyBoardAdapter.java: -------------------------------------------------------------------------------- 1 | package com.lnyp.pswkeyboard.adapter; 2 | 3 | import android.content.Context; 4 | import android.graphics.Color; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.BaseAdapter; 8 | import android.widget.RelativeLayout; 9 | import android.widget.TextView; 10 | 11 | import com.lnyp.pswkeyboard.R; 12 | 13 | import java.util.ArrayList; 14 | import java.util.Map; 15 | 16 | /** 17 | * 九宫格键盘适配器 18 | */ 19 | public class KeyBoardAdapter extends BaseAdapter { 20 | 21 | 22 | private Context mContext; 23 | private ArrayList> valueList; 24 | 25 | public KeyBoardAdapter(Context mContext, ArrayList> valueList) { 26 | this.mContext = mContext; 27 | this.valueList = valueList; 28 | } 29 | 30 | @Override 31 | public int getCount() { 32 | return valueList.size(); 33 | } 34 | 35 | @Override 36 | public Object getItem(int position) { 37 | return valueList.get(position); 38 | } 39 | 40 | @Override 41 | public long getItemId(int position) { 42 | return position; 43 | } 44 | 45 | @Override 46 | public View getView(int position, View convertView, ViewGroup parent) { 47 | ViewHolder viewHolder; 48 | if (convertView == null) { 49 | convertView = View.inflate(mContext, R.layout.grid_item_virtual_keyboard, null); 50 | viewHolder = new ViewHolder(); 51 | viewHolder.btnKey = (TextView) convertView.findViewById(R.id.btn_keys); 52 | viewHolder.imgDelete = (RelativeLayout) convertView.findViewById(R.id.imgDelete); 53 | 54 | convertView.setTag(viewHolder); 55 | } else { 56 | viewHolder = (ViewHolder) convertView.getTag(); 57 | } 58 | 59 | if (position == 9) { 60 | viewHolder.imgDelete.setVisibility(View.INVISIBLE); 61 | viewHolder.btnKey.setVisibility(View.VISIBLE); 62 | viewHolder.btnKey.setText(valueList.get(position).get("name")); 63 | viewHolder.btnKey.setBackgroundColor(Color.parseColor("#e0e0e0")); 64 | } else if (position == 11) { 65 | viewHolder.btnKey.setBackgroundResource(R.mipmap.keyboard_delete_img); 66 | viewHolder.imgDelete.setVisibility(View.VISIBLE); 67 | viewHolder.btnKey.setVisibility(View.INVISIBLE); 68 | 69 | } else { 70 | viewHolder.imgDelete.setVisibility(View.INVISIBLE); 71 | viewHolder.btnKey.setVisibility(View.VISIBLE); 72 | 73 | viewHolder.btnKey.setText(valueList.get(position).get("name")); 74 | } 75 | 76 | return convertView; 77 | } 78 | 79 | /** 80 | * 存放控件 81 | */ 82 | public final class ViewHolder { 83 | public TextView btnKey; 84 | public RelativeLayout imgDelete; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /app/src/main/java/com/lnyp/pswkeyboard/widget/PasswordView.java: -------------------------------------------------------------------------------- 1 | package com.lnyp.pswkeyboard.widget; 2 | 3 | import android.content.Context; 4 | import android.text.Editable; 5 | import android.text.TextWatcher; 6 | import android.util.AttributeSet; 7 | import android.view.View; 8 | import android.widget.AdapterView; 9 | import android.widget.GridView; 10 | import android.widget.ImageView; 11 | import android.widget.RelativeLayout; 12 | import android.widget.TextView; 13 | 14 | import com.lnyp.pswkeyboard.OnPasswordInputFinish; 15 | import com.lnyp.pswkeyboard.R; 16 | import com.lnyp.pswkeyboard.adapter.KeyBoardAdapter; 17 | 18 | import java.util.ArrayList; 19 | import java.util.HashMap; 20 | import java.util.Map; 21 | 22 | /** 23 | * 弹框里面的View 24 | */ 25 | public class PasswordView extends RelativeLayout { 26 | 27 | Context mContext; 28 | 29 | private VirtualKeyboardView virtualKeyboardView; 30 | 31 | private TextView[] tvList; //用数组保存6个TextView,为什么用数组? 32 | 33 | private ImageView[] imgList; //用数组保存6个TextView,为什么用数组? 34 | 35 | private GridView gridView; 36 | 37 | private ImageView imgCancel; 38 | 39 | private ArrayList> valueList; 40 | 41 | private int currentIndex = -1; //用于记录当前输入密码格位置 42 | 43 | public PasswordView(Context context) { 44 | this(context, null); 45 | } 46 | 47 | public PasswordView(Context context, AttributeSet attrs) { 48 | super(context, attrs); 49 | this.mContext = context; 50 | 51 | View view = View.inflate(context, R.layout.layout_popup_bottom, null); 52 | 53 | virtualKeyboardView = (VirtualKeyboardView) view.findViewById(R.id.virtualKeyboardView); 54 | imgCancel = (ImageView) view.findViewById(R.id.img_cancel); 55 | gridView = virtualKeyboardView.getGridView(); 56 | 57 | initValueList(); 58 | 59 | initView(view); 60 | 61 | setupView(); 62 | 63 | addView(view); 64 | } 65 | 66 | private void initView(View view) { 67 | 68 | 69 | tvList = new TextView[6]; 70 | 71 | imgList = new ImageView[6]; 72 | 73 | tvList[0] = (TextView) view.findViewById(R.id.tv_pass1); 74 | tvList[1] = (TextView) view.findViewById(R.id.tv_pass2); 75 | tvList[2] = (TextView) view.findViewById(R.id.tv_pass3); 76 | tvList[3] = (TextView) view.findViewById(R.id.tv_pass4); 77 | tvList[4] = (TextView) view.findViewById(R.id.tv_pass5); 78 | tvList[5] = (TextView) view.findViewById(R.id.tv_pass6); 79 | 80 | 81 | imgList[0] = (ImageView) view.findViewById(R.id.img_pass1); 82 | imgList[1] = (ImageView) view.findViewById(R.id.img_pass2); 83 | imgList[2] = (ImageView) view.findViewById(R.id.img_pass3); 84 | imgList[3] = (ImageView) view.findViewById(R.id.img_pass4); 85 | imgList[4] = (ImageView) view.findViewById(R.id.img_pass5); 86 | imgList[5] = (ImageView) view.findViewById(R.id.img_pass6); 87 | 88 | } 89 | 90 | // 这里,我们没有使用默认的数字键盘,因为第10个数字不显示.而是空白 91 | private void initValueList() { 92 | 93 | valueList = new ArrayList<>(); 94 | 95 | // 初始化按钮上应该显示的数字 96 | for (int i = 1; i < 13; i++) { 97 | Map map = new HashMap(); 98 | if (i < 10) { 99 | map.put("name", String.valueOf(i)); 100 | } else if (i == 10) { 101 | map.put("name", ""); 102 | } else if (i == 11) { 103 | map.put("name", String.valueOf(0)); 104 | } else if (i == 12) { 105 | map.put("name", ""); 106 | } 107 | valueList.add(map); 108 | } 109 | } 110 | 111 | private void setupView() { 112 | 113 | // 这里、重新为数字键盘gridView设置了Adapter 114 | KeyBoardAdapter keyBoardAdapter = new KeyBoardAdapter(mContext, valueList); 115 | gridView.setAdapter(keyBoardAdapter); 116 | 117 | gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 118 | @Override 119 | public void onItemClick(AdapterView parent, View view, int position, long id) { 120 | if (position < 11 && position != 9) { //点击0~9按钮 121 | 122 | if (currentIndex >= -1 && currentIndex < 5) { //判断输入位置————要小心数组越界 123 | ; 124 | ++currentIndex; 125 | tvList[currentIndex].setText(valueList.get(position).get("name")); 126 | 127 | tvList[currentIndex].setVisibility(View.INVISIBLE); 128 | imgList[currentIndex].setVisibility(View.VISIBLE); 129 | } 130 | } else { 131 | if (position == 11) { //点击退格键 132 | if (currentIndex - 1 >= -1) { //判断是否删除完毕————要小心数组越界 133 | 134 | tvList[currentIndex].setText(""); 135 | 136 | tvList[currentIndex].setVisibility(View.VISIBLE); 137 | imgList[currentIndex].setVisibility(View.INVISIBLE); 138 | 139 | currentIndex--; 140 | } 141 | } 142 | } 143 | } 144 | }); 145 | } 146 | 147 | //设置监听方法,在第6位输入完成后触发 148 | public void setOnFinishInput(final OnPasswordInputFinish pass) { 149 | 150 | 151 | tvList[5].addTextChangedListener(new TextWatcher() { 152 | @Override 153 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { 154 | 155 | } 156 | 157 | @Override 158 | public void onTextChanged(CharSequence s, int start, int before, int count) { 159 | 160 | } 161 | 162 | @Override 163 | public void afterTextChanged(Editable s) { 164 | 165 | if (s.toString().length() == 1) { 166 | 167 | String strPassword = ""; //每次触发都要先将strPassword置空,再重新获取,避免由于输入删除再输入造成混乱 168 | 169 | for (int i = 0; i < 6; i++) { 170 | strPassword += tvList[i].getText().toString().trim(); 171 | } 172 | pass.inputFinish(strPassword); //接口中要实现的方法,完成密码输入完成后的响应逻辑 173 | } 174 | } 175 | }); 176 | } 177 | 178 | public VirtualKeyboardView getVirtualKeyboardView() { 179 | 180 | return virtualKeyboardView; 181 | } 182 | 183 | public ImageView getImgCancel() { 184 | return imgCancel; 185 | } 186 | } 187 | -------------------------------------------------------------------------------- /app/src/main/java/com/lnyp/pswkeyboard/widget/PopEnterPassword.java: -------------------------------------------------------------------------------- 1 | package com.lnyp.pswkeyboard.widget; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.graphics.drawable.ColorDrawable; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup.LayoutParams; 9 | import android.widget.PopupWindow; 10 | import android.widget.Toast; 11 | 12 | import com.lnyp.pswkeyboard.OnPasswordInputFinish; 13 | import com.lnyp.pswkeyboard.R; 14 | 15 | 16 | /** 17 | * 输入支付密码 18 | * 19 | * @author lining 20 | */ 21 | public class PopEnterPassword extends PopupWindow { 22 | 23 | private PasswordView pwdView; 24 | 25 | private View mMenuView; 26 | 27 | private Activity mContext; 28 | 29 | public PopEnterPassword(final Activity context) { 30 | 31 | super(context); 32 | 33 | this.mContext = context; 34 | 35 | LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 36 | 37 | mMenuView = inflater.inflate(R.layout.pop_enter_password, null); 38 | 39 | pwdView = (PasswordView) mMenuView.findViewById(R.id.pwd_view); 40 | 41 | //添加密码输入完成的响应 42 | pwdView.setOnFinishInput(new OnPasswordInputFinish() { 43 | @Override 44 | public void inputFinish(final String password) { 45 | 46 | new Thread(new Runnable() { 47 | 48 | @Override 49 | public void run() { 50 | // 模拟耗时的操作。 51 | try { 52 | 53 | Thread.sleep(500); 54 | 55 | } catch (InterruptedException e) { 56 | e.printStackTrace(); 57 | } 58 | 59 | mContext.runOnUiThread(new Runnable() { 60 | 61 | @Override 62 | public void run() { 63 | 64 | dismiss(); 65 | 66 | Toast.makeText(mContext, "支付成功,密码为:" + password, Toast.LENGTH_SHORT).show(); 67 | } 68 | }); 69 | } 70 | 71 | }).start(); 72 | } 73 | }); 74 | 75 | // 监听X关闭按钮 76 | pwdView.getImgCancel().setOnClickListener(new View.OnClickListener() { 77 | @Override 78 | public void onClick(View v) { 79 | dismiss(); 80 | } 81 | }); 82 | 83 | // 监听键盘上方的返回 84 | pwdView.getVirtualKeyboardView().getLayoutBack().setOnClickListener(new View.OnClickListener() { 85 | @Override 86 | public void onClick(View v) { 87 | dismiss(); 88 | } 89 | }); 90 | 91 | // 设置SelectPicPopupWindow的View 92 | this.setContentView(mMenuView); 93 | // 设置SelectPicPopupWindow弹出窗体的宽 94 | this.setWidth(LayoutParams.MATCH_PARENT); 95 | // 设置SelectPicPopupWindow弹出窗体的高 96 | this.setHeight(LayoutParams.WRAP_CONTENT); 97 | // 设置SelectPicPopupWindow弹出窗体可点击 98 | this.setFocusable(true); 99 | // 设置SelectPicPopupWindow弹出窗体动画效果 100 | this.setAnimationStyle(R.style.pop_add_ainm); 101 | // 实例化一个ColorDrawable颜色为半透明 102 | ColorDrawable dw = new ColorDrawable(0x66000000); 103 | // 设置SelectPicPopupWindow弹出窗体的背景 104 | this.setBackgroundDrawable(dw); 105 | 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /app/src/main/java/com/lnyp/pswkeyboard/widget/VirtualKeyboardView.java: -------------------------------------------------------------------------------- 1 | package com.lnyp.pswkeyboard.widget; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.view.View; 6 | import android.widget.GridView; 7 | import android.widget.RelativeLayout; 8 | 9 | import com.lnyp.pswkeyboard.R; 10 | import com.lnyp.pswkeyboard.adapter.KeyBoardAdapter; 11 | 12 | import java.util.ArrayList; 13 | import java.util.HashMap; 14 | import java.util.Map; 15 | 16 | /** 17 | * 虚拟键盘 18 | */ 19 | public class VirtualKeyboardView extends RelativeLayout { 20 | 21 | Context context; 22 | 23 | //因为就6个输入框不会变了,用数组内存申请固定空间,比List省空间(自己认为) 24 | private GridView gridView; //用GrideView布局键盘,其实并不是真正的键盘,只是模拟键盘的功能 25 | 26 | private ArrayList> valueList; //有人可能有疑问,为何这里不用数组了? 27 | //因为要用Adapter中适配,用数组不能往adapter中填充 28 | 29 | private RelativeLayout layoutBack; 30 | 31 | public VirtualKeyboardView(Context context) { 32 | this(context, null); 33 | } 34 | 35 | public VirtualKeyboardView(Context context, AttributeSet attrs) { 36 | super(context, attrs); 37 | 38 | this.context = context; 39 | 40 | View view = View.inflate(context, R.layout.layout_virtual_keyboard, null); 41 | 42 | valueList = new ArrayList<>(); 43 | 44 | layoutBack = (RelativeLayout) view.findViewById(R.id.layoutBack); 45 | 46 | gridView = (GridView) view.findViewById(R.id.gv_keybord); 47 | 48 | initValueList(); 49 | 50 | setupView(); 51 | 52 | addView(view); //必须要,不然不显示控件 53 | } 54 | 55 | public RelativeLayout getLayoutBack() { 56 | return layoutBack; 57 | } 58 | 59 | public ArrayList> getValueList() { 60 | return valueList; 61 | } 62 | 63 | private void initValueList() { 64 | 65 | // 初始化按钮上应该显示的数字 66 | for (int i = 1; i < 13; i++) { 67 | Map map = new HashMap<>(); 68 | if (i < 10) { 69 | map.put("name", String.valueOf(i)); 70 | } else if (i == 10) { 71 | map.put("name", "."); 72 | } else if (i == 11) { 73 | map.put("name", String.valueOf(0)); 74 | } else if (i == 12) { 75 | map.put("name", ""); 76 | } 77 | valueList.add(map); 78 | } 79 | } 80 | 81 | public GridView getGridView() { 82 | return gridView; 83 | } 84 | 85 | private void setupView() { 86 | 87 | KeyBoardAdapter keyBoardAdapter = new KeyBoardAdapter(context, valueList); 88 | gridView.setAdapter(keyBoardAdapter); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /app/src/main/res/anim/push_bottom_in.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 15 | -------------------------------------------------------------------------------- /app/src/main/res/anim/push_bottom_out.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 10 | 15 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/img_psw_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/psw_input_area_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/selector_gird_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 |