本文主要是介绍根据输入的银行卡号显示开户银行类型,并将银行卡号四位分隔,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
工作中遇到将绑定银行卡的需求,而且需要根据银行卡号显示开户行类型,并将银行卡号四位分隔,参考了下网上的栗子,并进行了融合优化,写出适合自己的代码
不多说,直接上代码,注释写的很清晰。
1、Activity逻辑实现
package net.edaibu.banknametest;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {int beforeTextLength = 0;int onTextLength = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView() {final EditText tvBankNamePrinted = (EditText) findViewById(R.id.tv_name_print);final TextView tvDescribeBankName = (TextView) findViewById(R.id.tv_describe_bank_name);tvBankNamePrinted.addTextChangedListener(new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {beforeTextLength = s.length();}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) {Log.e("tag", "输入的数字:" + s);String bankNumber = tvBankNamePrinted.getText().toString().trim();onTextLength = bankNumber.length();if (onTextLength > beforeTextLength) {if (bankNumber.length() == 5 || bankNumber.length() == 10 || bankNumber.length() == 15 || bankNumber.length() == 20) {tvBankNamePrinted.setText(new StringBuffer(bankNumber).insert(bankNumber.length() - 1, " ").toString());tvBankNamePrinted.setSelection(tvBankNamePrinted.getText().length());}} else {if (bankNumber.startsWith(" ")) {tvBankNamePrinted.setText(new StringBuffer(bankNumber).delete(onTextLength - 1, onTextLength).toString());tvBankNamePrinted.setSelection(tvBankNamePrinted.getText().length());}}//根据银行卡号前6位查询银行信息if (bankNumber.length() >= 7) {//移除空格String noSpaceNumber = removeAllSpace(bankNumber);//截取前6位String divideStr = noSpaceNumber.substring(0, 6);//通过银行卡号前6位查询开户行信息String bankName = GetBankNameUtil.getNameOfBank(MainActivity.this, Long.parseLong(divideStr));tvDescribeBankName.setText(bankName);} else {tvDescribeBankName.setText("");}}});}/*** 移除空格* @param str* @return*/public String removeAllSpace(String str) {String tmpStr = str.replace(" ", "");return tmpStr;}}
2、layout布局
<?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"><RelativeLayoutandroid:id="@+id/rl_name"android:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:id="@+id/tv_name"android:layout_width="wrap_content"android:layout_height="40dp"android:text="银行卡号"android:layout_margin="8dp"android:gravity="center_vertical"android:textSize="16sp"android:textColor="#000000"/><EditTextandroid:id="@+id/tv_name_print"android:layout_width="match_parent"android:layout_height="40dp"android:layout_margin="8dp"android:layout_toRightOf="@+id/tv_name"android:background="@drawable/border_shape"android:hint="请输入银行卡号"/></RelativeLayout><TextViewandroid:id="@+id/tv_describe_bank_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="16sp"android:textColor="#ff00ff"android:layout_below="@+id/rl_name"android:text=""/></RelativeLayout>
3、根据银行卡号查询银行信息的工具类
package net.edaibu.banknametest;import android.content.Context;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;/*** @author geqipeng* @date 2017/8/10*/public class GetBankNameUtil {private static String bankName;private static String binName;private static String openBinNum(Context context) {ByteArrayOutputStream outputStream = new ByteArrayOutputStream();String str = null;try {//打开资源目录文件,获取银行卡号对应的查询库InputStream is = context.getResources().getAssets().open("binNum.txt");byte[] bytes = new byte[1024];int length = 0;while ((length = is.read(bytes)) != -1) {outputStream.write(bytes, 0, length);}is.close();outputStream.close();str = outputStream.toString();} catch (IOException e) {e.printStackTrace();}return str;}//获得Bank card的前缀private static List<Long> getBinNum(Context context) {String binNum = openBinNum(context);String[] binArr = binNum.split(",");List<Long> lon = new ArrayList<>();for (int i = 0; i < binArr.length; i++) {if (i % 2 == 0)lon.add(Long.parseLong(binArr[i]));}return lon;}//获得BankNameprivate static List<String> getBinName(Context context) {String binNum = openBinNum(context);String[] binArr = binNum.split(",");List<String> list = new ArrayList<>();for (int i = 0; i < binArr.length; i++) {if (i % 2 != 0) {if (binArr[i].contains("-")) {//截取首尾部分bankName = binArr[i].substring(0, binArr[i].indexOf("-")) + binArr[i].substring(binArr[i].lastIndexOf("-"));} else {bankName = binArr[i];}list.add(bankName);}}return list;}//通过输入的卡号获得银行卡信息public static String getNameOfBank(Context context, long binNum) {int index = 0;index = binarySearch(getBinNum(context), binNum);//银行名if (index!=-1){binName = getBinName(context).get(index);return binName + "\n";}return "未查询到相关银行信息\n";}//数量有上千条,利用二分查找算法来进行快速查找法public static int binarySearch(List<Long> srcArray, long des) {int low = 0;int high = srcArray.size() - 1;while (low <= high) {int middle = (low + high) / 2;if (des == srcArray.get(middle)) {return middle;} else if (des < srcArray.get(middle)) {high = middle - 1;} else {low = middle + 1;}}return -1;}
}
4、效果图
感谢:
http://blog.csdn.net/u011780646/article/details/42557683
http://blog.csdn.net/u013334392/article/details/52511743
银行卡号查询文档 binNumber.txt下载链接:https://pan.baidu.com/s/1dFkvtg1 密码:kuug
这篇关于根据输入的银行卡号显示开户银行类型,并将银行卡号四位分隔的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!