今天突然想起以前斗地主的时候老记不住牌,于是乎想到做一个记牌器,为了方便记牌,需要自己定义一个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;//更新ListViewprivate Handler hand=new Handler(){@Overridepublic 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));//激活keyboardViewkeyboardView.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);  }  }  @Overridepublic 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;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// TODO Auto-generated method stubString title=item.getTitle().toString();;if (title.equals("斗地主")) {faDouDiZhuPai();}else{faPaoDeKuaiPai();}return super.onOptionsItemSelected(item);}private void faPaoDeKuaiPai() {// TODO Auto-generated method stubpai_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 stubpai_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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubeditable=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
        }  };}

转载于:https://www.cnblogs.com/wushunlin287/p/3218816.html

android定义键盘示例(斗地主或跑得快的记牌器)相关推荐

  1. 基于opencv2的斗地主记牌器(python)

    一.开发该系统软件环境及使用的技术说明 开发环境:JetBrains PyCharm Community Edition 2019.2.5 x64 远端仓库:GitHub 语言版本:python3.7 ...

  2. JJ斗地主记牌器java开发_【欢乐斗地主记牌器制作】遇到两个问题

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 本菜笔准备用swing做个记牌器,但是遇到俩问题,求助大神. 先抛出最简单的问题:点击button1文本框textfield1中数字会减一,但是我怎么实现 ...

  3. Android 平板中 自己定义键盘(popuwindow) 居于屏幕左下方 仿微信的password输入界面...

    之前博客中,介绍过使用谷歌提供的键盘的一些api,能够非常好地自己定义键盘,參考我之前的博客链接:android 自己定义键盘 ,这个有一个局限性,仅仅能占满屏幕,无法做到仅仅能占一部分的需求键盘,例 ...

  4. android 设置键盘弹出动画,Android实现键盘弹出界面上移的实现思路

    1.首先说一下思路: 基本就是结合layout中ScrollView视图和AndroidManifest.xml中activity中的android:windowSoftInputMode属性配置实现 ...

  5. android 自动 键盘,关于Android中的软键盘

    InputMethodService为我们的输入法创建了一个Dialog,并且将该Dialog的Window的某些参数(如Gravity)进行了设置,使之能够在底部或者全屏显示.当我们点击输入框时,系 ...

  6. android自定义键盘遮挡,Android软键盘遮挡的四种完美解决方案

    一.问题概述 在编辑框输入内容时会弹出软键盘,而手机屏幕区域有限往往会遮住输入界面,我们先看一下问题效果图: 输入用户名和密码时,系统会弹出键盘,造成系统键盘会挡住文本框的问题,如图所示: 输入密码时 ...

  7. android布局参照示例_约束布局Android示例–第2部分

    android布局参照示例 This is the second tutorial in the series of posts on Constraint Layout android exampl ...

  8. Android 自定义键盘 随机键盘

    之前上传的另外一个自定义键盘,并没有实现键盘弹出的时候,布局向上自动调整.(网络上所有的自定义键盘都没有添加自适应的功能,而且布局没这么好看,上一个例子资源链接为:http://download.cs ...

  9. Arduino Mega/Uno制作宏定义键盘(不需刷firmware!!)

    本人机械专业大一编程小白一枚,接触编程不到半年.近期开始自学Arduino.这应该是本人第一篇博客,有错误之处敬请斧正! - 心路&思路 家住武汉,这个年嘛,大家应该都懂,被迫宅化.年前从网上 ...

  10. Android 软键盘功能键(EditText)

    夜深了.废话不多说了,项目需要改变Android软键盘右角下的功能键! 好了!先看图?还是代码?.... 还是先代码.然后效果图! 代码: public class MainActivity exte ...

最新文章

  1. Android编译系统中的Kconfig,Makefile,.config编译系统浅析
  2. ES6基础3(扩展)-学习笔记
  3. 【Linux】crontab 定时启动sh
  4. Python——如何将不规范的英文名字转化为“首字母大写,其他字母小写”的规范名字
  5. SQL:postgresql中st_union合并多条geom数据
  6. tas5424_TAS5424
  7. Error response from daemon: conflict: unable to delete feb5d9fea6a5 (must be forced)
  8. mysql choose when_mybatis使用choose when
  9. C#实现计算机远程操作
  10. 四舍五入函数round_如何在R中使用round()将数字四舍五入
  11. CF513C Maximum Subrectangle
  12. AI+智能服务机器人应用基础【实践报告】
  13. 微信支付应用签名修改后多久可以生效?
  14. 关于 Redis 的 懒惰删除 (十三)
  15. 隆重推荐:大明王朝1566
  16. 基于ESP8266的微信门铃
  17. 5G工作频段及波长覆盖计算
  18. 【迅为iMX6Q】开发板 Linux 5.15.71 RTL8211E 以太网驱动适配
  19. SLAMTEC-思岚科技产品动态|激光导航扫地机器人测试评估机SDP
  20. php的魔术方式包含哪些(越多越好)? 在何情况下被自动调用?,php笔试题及答案

热门文章

  1. 【H3CTE认证和HCIE认证一样吗?】
  2. Linux中常用查看日志命令
  3. AttributeError: module 'scipy.misc' has no attribute 'imrotate'
  4. 实验4——Pspice
  5. 图书销售系统系统设计说明书
  6. Dreamweaver实战技能从入门到精通-李炎恢-专题视频课程
  7. 【Git】Failed to connect to github.com port 443 after 21092 ms: Connection refused
  8. 数学分析教程(科大)——4.2笔记+习题
  9. rs232接口_RS232接口与RS485究竟有什么区别?
  10. 这几天阅读的shadowgun的几个shader