本篇文章為大家展示了Android中怎么實(shí)現(xiàn)微信支付數(shù)字鍵盤功能,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來,先為鄒城等服務(wù)建站,鄒城等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為鄒城企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
1. 自定義布局
鍵盤的布局,實(shí)質(zhì)就是一個(gè)4X3網(wǎng)格布局的GridView。
2.實(shí)現(xiàn)數(shù)字鍵盤內(nèi)容
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
看下適配器如何處理: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; /** * 九宮格鍵盤適配器 */ public class KeyBoardAdapter extends BaseAdapter { private Context mContext; private ArrayList> valueList; public KeyBoardAdapter(Context mContext, ArrayList > 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; } }
在看Adapter之前,我們先看下grid_item_virtual_keyboard是如何實(shí)現(xiàn)的:
可以看到,我們在item布局文件中,指定了兩個(gè)view,一個(gè)是普通顯示數(shù)字的TextView, 一個(gè)是顯示最后刪除鍵的RelativeLayout。
然后,在KeyBoardAdapter 的getView方法中,我們根據(jù)position位置,對布局進(jìn)行不同的處理。當(dāng)position為9,也就是倒數(shù)第三個(gè)按鍵,它的按鈕顏色要單獨(dú)設(shè)置。 當(dāng)position為12也就是最后一個(gè)按鈕時(shí),需要控制刪除按鈕顯示,數(shù)字按鈕隱藏。 其余情況則是刪除按鈕隱藏,數(shù)字按鈕顯示。
3.使用并實(shí)現(xiàn)鍵盤事件邏輯
布局中,可以直接使用自己定義的數(shù)字鍵盤:
我們在Activity中,操作數(shù)字鍵盤:
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 gridView; private ArrayList> valueList; private EditText textAmount; private Animation enterAnim; private Animation exitAnim; @Override protected 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() { @Override public void onClick(View v) { virtualKeyboardView.startAnimation(exitAnim); virtualKeyboardView.setVisibility(View.GONE); } }); gridView = virtualKeyboardView.getGridView(); gridView.setOnItemClickListener(onItemClickListener); textAmount.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { virtualKeyboardView.setFocusable(true); virtualKeyboardView.setFocusableInTouchMode(true); virtualKeyboardView.startAnimation(enterAnim); virtualKeyboardView.setVisibility(View.VISIBLE); } }); } private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> adapterView, View view, int position, long l) { if (position < 11 && position != 9) { //點(diǎn)擊0~9按鈕 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) { //點(diǎn)擊退格鍵 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) { //點(diǎn)擊退格鍵 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()); } } } } };}
上述內(nèi)容就是Android中怎么實(shí)現(xiàn)微信支付數(shù)字鍵盤功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。