android定义键盘示例(斗地主或跑得快的记牌器)
今天突然想起以前斗地主的时候老记不住牌,于是乎想到做一个记牌器,为了方便记牌,需要自己定义一个android键盘,于是学习了一下利用android.inputmethodservice.Keyboard 来自己定义了一个软键盘效果如图所示:
在测试时发现预览框里面没有显示我们输入的内容,网上查找原因和浏览源代码都找不到解决方案,然后只好逐个测试,最后发现时程序默认主题覆盖使得提示框的字体颜色和背景颜色一致,把主题去掉或换一个主题即可解决
还有当输入框为密码框时,需要加入edit.setTransformationMethod(PasswordTransformationMethod.getInstance());否则输入显示的是明文
具体实现:
keycontent.xml这个文件定义了键盘的布局
<?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyWidth="25%p" android:horizontalGap="0px" android:verticalGap="0px" android:keyHeight="50dip"> <Row> <Key android:codes="51" android:keyLabel="3" /> <Key android:codes="52" android:keyLabel="4" /> <Key android:codes="53" android:keyLabel="5" /> <Key android:codes="54" android:keyLabel="6" /> <!-- <Key android:codes="57419" android:keyEdgeFlags="right" android:keyIcon="@drawable/ic_launcher" /> --> </Row> <Row> <Key android:codes="55" android:keyLabel="7" /> <Key android:codes="56" android:keyLabel="8" /> <Key android:codes="57" android:keyLabel="9" /> <Key android:codes="1010" android:keyLabel="10" android:keyEdgeFlags="right" android:isRepeatable="true" /> </Row> <Row> <Key android:codes="74" android:keyLabel="J" /> <Key android:codes="81" android:keyLabel="Q" /> <Key android:codes="75" android:keyLabel="K" /> <Key android:codes="65" android:keyLabel="A" android:keyEdgeFlags="right" android:isRepeatable="true" /> </Row> <Row> <Key android:codes="-3" android:keyIcon="@drawable/sym_keyboard_left" /> <Key android:codes="1070" android:keyLabel="小王" /> <Key android:codes="1080" android:keyLabel="大王" /> <Key android:codes="50" android:keyLabel="2" android:keyEdgeFlags="right" android:isRepeatable="true" /> </Row> </Keyboard>
keyboardview.xml这个是主程序界面布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="50dp" > <EditText android:id="@+id/edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="true" android:layout_weight="1" /> <Button android:id="@+id/bt_go" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="2.5" android:text="GO"/> <Button android:id="@+id/bt_delete" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="2.5" android:text="删除"/> </LinearLayout> <ListView android:id="@+id/lv_pai" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"/>" <com.example.jipaiqi.MyKeboardView android:id="@+id/keyboard_view" android:visibility="gone" android:focusable="true" android:focusableInTouchMode="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0" /> </LinearLayout>
程序代码:
import java.util.LinkedHashMap; import java.util.Set; import android.app.Activity; import android.inputmethodservice.Keyboard; import android.inputmethodservice.KeyboardView; import android.inputmethodservice.KeyboardView.OnKeyboardActionListener; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.Editable; import android.text.InputType; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListAdapter; import android.widget.ListView; public class MainActivity extends Activity { private EditText edit; private KeyboardView keyboardView; private Button bt_go; private Button bt_delete; private Editable editable; //用LinkedHashMap记录剩余的牌,用牌的名称做键,剩余张数做值 private LinkedHashMap<String,Integer> pai_content; private ListView lv_pai; private ListAdapter adapter; //更新ListView private Handler hand=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub lv_pai.setAdapter(adapter); super.handleMessage(msg); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.keyboardview); edit = (EditText)findViewById(R.id.edit); //设置InputType.TYPE_NULL屏蔽默认的输入键盘 edit.setInputType(InputType.TYPE_NULL); //设置onclick事件当输入输入框被点击时弹出我们自定义的键盘 edit.setOnClickListener(editClickListener); bt_go=(Button) findViewById(R.id.bt_go); bt_delete=(Button) findViewById(R.id.bt_delete); bt_go.setOnClickListener(bt_goOnclickListener); bt_delete.setOnClickListener(bt_deleteOnclickListener); lv_pai=(ListView) findViewById(R.id.lv_pai); //根据keyboard_view.xml生成keyboardview的对象 keyboardView = (MyKeboardView)findViewById(R.id.keyboard_view); //按照keycontent.xml配置内容生成Keyboard加入keyboardView的布局 keyboardView.setKeyboard(new Keyboard(this, R.xml.keycontent)); //激活keyboardView keyboardView.setEnabled(true); //设置keboardview为带提示的 keyboardView.setPreviewEnabled(true); //设置OnkeyBoardActionListener在里面处理输入内容 keyboardView.setOnKeyboardActionListener(onKeyBoardActionListener); } private void showKeyboard() { int visibility = keyboardView.getVisibility(); if (visibility == View.GONE || visibility == View.INVISIBLE) { keyboardView.setVisibility(View.VISIBLE); } } private void hideKeyboard() { int visibility = keyboardView.getVisibility(); if (visibility == View.VISIBLE) { keyboardView.setVisibility(View.GONE); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub String title=item.getTitle().toString();; if (title.equals("斗地主")) { faDouDiZhuPai(); }else{ faPaoDeKuaiPai(); } return super.onOptionsItemSelected(item); } private void faPaoDeKuaiPai() { // TODO Auto-generated method stub pai_content=new LinkedHashMap<String,Integer>(); pai_content.put("2", 1); pai_content.put("A", 3); pai_content.put("K", 4); pai_content.put("Q", 4); pai_content.put("J", 4); pai_content.put("10", 4); pai_content.put("9", 4); pai_content.put("8", 4); pai_content.put("7", 4); pai_content.put("6", 4); pai_content.put("5", 4); pai_content.put("4", 4); pai_content.put("3", 4); genXinListView(pai_content); } private void faDouDiZhuPai() { // TODO Auto-generated method stub pai_content=new LinkedHashMap<String,Integer>(); pai_content.put("大王", 1); pai_content.put("小王", 1); pai_content.put("2", 4); pai_content.put("A", 4); pai_content.put("K", 4); pai_content.put("Q", 4); pai_content.put("J", 4); pai_content.put("10", 4); pai_content.put("9", 4); pai_content.put("8", 4); pai_content.put("7", 4); pai_content.put("6", 4); pai_content.put("5", 4); pai_content.put("4", 4); pai_content.put("3", 4); genXinListView(pai_content); } private void genXinListView(LinkedHashMap<String, Integer> data){ String shenyupai[]=new String[data.size()]; Set<String> set=data.keySet(); int i=0; for (String string : set) { shenyupai[i]=data.get(string)+"张"+string; i++; } adapter=new ArrayAdapter(this, android.R.layout.simple_list_item_1, shenyupai); Message msg=new Message(); hand.sendMessage(msg); } private OnClickListener editClickListener=new OnClickListener() { @Override public void onClick(View v) { showKeyboard(); } }; private OnClickListener bt_goOnclickListener=new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (pai_content==null)return; String inputStr=edit.getText().toString().trim(); String xiapai[]=inputStr.split("\\|"); for (int i = 0; i < xiapai.length; i++) { System.out.println(xiapai[i]); if (pai_content.get(xiapai[i])!=null) { int index=pai_content.get(xiapai[i]); index--; System.out.println(xiapai[i]); if (index>0) { pai_content.put(xiapai[i], index); }else{ pai_content.remove(xiapai[i]); } } } genXinListView(pai_content); edit.setText(""); } }; private OnClickListener bt_deleteOnclickListener=new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub editable=edit.getText(); int start = edit.getSelectionStart(); if (start>0) { editable.delete(start-1, start); } } }; private OnKeyboardActionListener onKeyBoardActionListener =new OnKeyboardActionListener() { @Override public void onKey(int primaryCode, int[] keyCodes) { editable=edit.getText(); int start = edit.getSelectionStart(); if (primaryCode == Keyboard.KEYCODE_CANCEL) { hideKeyboard(); } else if (primaryCode == 1070) { // go left editable.insert(start, "小王|"); } else if (primaryCode == 1080) { // go left editable.insert(start, "大王|"); }else if (primaryCode == 1010) { // go left editable.insert(start, "10|"); } else { editable.insert(start, Character.toString((char)primaryCode)+"|"); } } @Override public void onPress(int primaryCode) { // TODO Auto-generated method stub } @Override public void onRelease(int primaryCode) { // TODO Auto-generated method stub } @Override public void onText(CharSequence text) { // TODO Auto-generated method stub } @Override public void swipeDown() { // TODO Auto-generated method stub } @Override public void swipeLeft() { // TODO Auto-generated method stub } @Override public void swipeRight() { // TODO Auto-generated method stub } @Override public void swipeUp() { // TODO Auto-generated method stub } }; }