<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" > <EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginRight="24dp"
android:ems="10"
android:inputType="none" > <requestFocus />
</EditText> <android.inputmethodservice.KeyboardView
android:id="@+id/keyboardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="5.0dp"
android:background="#ffffffff"
android:focusable="true"
android:keyBackground="@drawable/calculator_button_bg"
android:keyTextColor="@color/black"
android:keyTextSize="26.0sp"
android:shadowColor="#ffffffff"
android:shadowRadius="0.0"
android:visibility="gone" /> </RelativeLayout>
hexkbd.xml
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffffff"
android:keyHeight="10%p"
android:keyWidth="100%p" > <Row>
<Key
android:codes="55"
android:keyEdgeFlags="left"
android:keyLabel="7" />
<Key
android:codes="56"
android:keyLabel="8" />
<Key
android:codes="57"
android:keyLabel="9" />
</Row>
<Row>
<Key
android:codes="52"
android:keyEdgeFlags="left"
android:keyLabel="4" />
<Key
android:codes="53"
android:keyLabel="5" />
<Key
android:codes="54"
android:keyLabel="6" />
</Row>
<Row>
<Key
android:codes="49"
android:keyEdgeFlags="left"
android:keyLabel="1" />
<Key
android:codes="50"
android:keyLabel="2" />
<Key
android:codes="51"
android:keyLabel="3" />
</Row>
<Row>
<Key
android:codes="46"
android:keyEdgeFlags="left"
android:keyLabel="." />
<Key
android:codes="48"
android:keyLabel="0" />
<Key
android:codes="-5"
android:isRepeatable="true"
android:keyIcon="@drawable/sym_keyboard_delete" />
</Row> </Keyboard>
MainActivity.java
package com.example.yanlei.yl2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText; public class MainActivity extends Activity implements OnClickListener
{
CustomKeyboard mCustomKeyboard;
EditText editText; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText1);
mCustomKeyboard = new CustomKeyboard(this, R.id.keyboardview,
R.xml.hexkbd); mCustomKeyboard.registerEditText(R.id.editText1);
} @Override
public void onClick(View v)
{
switch (v.getId())
{
default:
break;
}
}
}
 CustomKeyboard.java
package com.example.yanlei.yl2;

import android.app.Activity;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText; /**
* When an activity hosts a keyboardView, this class allows several EditText's
* to register for it.
*
* @author Maarten Pennings
* @date 2012 December 23
*/
class CustomKeyboard
{ /** A link to the KeyboardView that is used to render this CustomKeyboard. */
private KeyboardView mKeyboardView;
/** A link to the activity that hosts the {@link #mKeyboardView}. */
private Activity mHostActivity; /** The key (code) handler. */
private OnKeyboardActionListener mOnKeyboardActionListener = new OnKeyboardActionListener()
{ public final static int CodeDelete = -5; // Keyboard.KEYCODE_DELETE
public final static int CodeCancel = -3; // Keyboard.KEYCODE_CANCEL @Override
public void onKey(int primaryCode, int[] keyCodes)
{
// NOTE We can say '<Key android:codes="49,50" ... >' in the xml
// file; all codes come in keyCodes, the first in this list in
// primaryCode
// Get the EditText and its Editable
View focusCurrent = mHostActivity.getWindow().getCurrentFocus();
if (focusCurrent == null
|| focusCurrent.getClass() != EditText.class)
return;
EditText edittext = (EditText) focusCurrent;
Editable editable = edittext.getText();
int start = edittext.getSelectionStart();
// Apply the key to the edittext
if (primaryCode == CodeCancel)
{
hideCustomKeyboard();
}
else if (primaryCode == CodeDelete)
{
if (editable != null && start > 0)
editable.delete(start - 1, start);
}
else
{ // insert character
editable.insert(start, Character.toString((char) primaryCode));
}
} @Override
public void onPress(int arg0)
{
} @Override
public void onRelease(int primaryCode)
{
} @Override
public void onText(CharSequence text)
{
} @Override
public void swipeDown()
{
} @Override
public void swipeLeft()
{
} @Override
public void swipeRight()
{
} @Override
public void swipeUp()
{
}
}; /**
* Create a custom keyboard, that uses the KeyboardView (with resource id
* <var>viewid</var>) of the <var>host</var> activity, and load the keyboard
* layout from xml file <var>layoutid</var> (see {@link Keyboard} for
* description). Note that the <var>host</var> activity must have a
* <var>KeyboardView</var> in its layout (typically aligned with the bottom
* of the activity). Note that the keyboard layout xml file may include key
* codes for navigation; see the constants in this class for their values.
* Note that to enable EditText's to use this custom keyboard, call the
* {@link #registerEditText(int)}.
*
* @param host
* The hosting activity.
* @param viewid
* The id of the KeyboardView.
* @param layoutid
* The id of the xml file containing the keyboard layout.
*/
public CustomKeyboard(Activity host, int viewid, int layoutid)
{
mHostActivity = host;
mKeyboardView = (KeyboardView) mHostActivity.findViewById(viewid);
mKeyboardView.setKeyboard(new Keyboard(mHostActivity, layoutid));
mKeyboardView.setPreviewEnabled(false); // NOTE Do not show the preview
// balloons
mKeyboardView.setOnKeyboardActionListener(mOnKeyboardActionListener);
// Hide the standard keyboard initially
mHostActivity.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
} /** Returns whether the CustomKeyboard is visible. */
public boolean isCustomKeyboardVisible()
{
return mKeyboardView.getVisibility() == View.VISIBLE;
} /**
* Make the CustomKeyboard visible, and hide the system keyboard for view v.
*/
public void showCustomKeyboard(View v)
{
mKeyboardView.setVisibility(View.VISIBLE);
mKeyboardView.setEnabled(true);
if (v != null)
((InputMethodManager) mHostActivity
.getSystemService(Activity.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(v.getWindowToken(), 0);
} /** Make the CustomKeyboard invisible. */
public void hideCustomKeyboard()
{
mKeyboardView.setVisibility(View.GONE);
mKeyboardView.setEnabled(false);
} /**
* Register <var>EditText<var> with resource id <var>resid</var> (on the
* hosting activity) for using this custom keyboard.
*
* @param resid
* The resource id of the EditText that registers to the custom
* keyboard.
*/
public void registerEditText(int resid)
{
// Find the EditText 'resid'
EditText edittext = (EditText) mHostActivity.findViewById(resid);
// Make the custom keyboard appear
edittext.setOnFocusChangeListener(new OnFocusChangeListener()
{
// NOTE By setting the on focus listener, we can show the custom
// keyboard when the edit box gets focus, but also hide it when the
// edit box loses focus
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
showCustomKeyboard(v);
else
hideCustomKeyboard();
}
});
edittext.setOnClickListener(new OnClickListener()
{
// NOTE By setting the on click listener, we can show the custom
// keyboard again, by tapping on an edit box that already had focus
// (but that had the keyboard hidden).
@Override
public void onClick(View v)
{
showCustomKeyboard(v);
}
});
// Disable standard keyboard hard way
// NOTE There is also an easy way:
// 'edittext.setInputType(InputType.TYPE_NULL)' (but you will not have a
// cursor, and no 'edittext.setCursorVisible(true)' doesn't work )
edittext.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
EditText edittext = (EditText) v;
int inType = edittext.getInputType(); // Backup the input type
edittext.setInputType(InputType.TYPE_NULL); // Disable standard
// keyboard
edittext.onTouchEvent(event); // Call native handler
edittext.setInputType(inType); // Restore input type
return true; // Consume touch event
}
});
// Disable spell check (hex strings look like words to Android)
edittext.setInputType(edittext.getInputType()
| InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
} } // NOTE How can we change the background color of some keys (like the
// shift/ctrl/alt)?
// NOTE What does android:keyEdgeFlags do/mean

Android 测试自定义纯数字软键盘的更多相关文章

  1. 自定义View 实现软键盘实现搜索

    1. xml文件中加入自定义 搜索view <com.etoury.etoury.ui.view.IconCenterEditText android:id="@+id/search_ ...

  2. Android之监听手机软键盘弹起与关闭

    背景: 在很多App开发过程中需要在Activity中监听Android设备的软键盘弹起与关闭,但是Android似乎没有提供相关的的监听API给我们来调用,本文提供了一个可行的办法来监听软键盘的弹起 ...

  3. Android 另类方法监听软键盘的弹出收起事件

    http://www.cnblogs.com/csonezp/p/5065624.html 最近做的项目碰到个问题,a界面是fragment+recyclerview,b界面带个edittext,并且 ...

  4. android:windowSoftInputMode属性详解 软键盘

    android:windowSoftInputMode activity主窗口与软键盘的交互模式,可以用来避免输入法面板遮挡问题,Android1.5后的一个新特性. 这个属性能影响两件事情: [一] ...

  5. Android 手动显示和隐藏软键盘

    1.方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) InputMethodManager imm = (InputMethodManager) getSystemService(Contex ...

  6. Android手动显示和隐藏软键盘

    1.方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) InputMethodManager imm = (InputMethodManager) getSystemService(Contex ...

  7. [项目总结]Android 手动显示和隐藏软键盘

    1.方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) 1 InputMethodManager imm = (InputMethodManager) getSystemService(Cont ...

  8. android开发之使edittext输入弹出数字软键盘。亲测可用。手机号登陆注册常用。

    <EditText android:id="@+id/edit_digit_input" android:layout_width="wrap_content&qu ...

  9. android edittext不弹出软键盘

    方法一: 在AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden 例如:< ...

随机推荐

  1. LA 7056 Colorful Toy Polya定理

    题意: 平面上给出一个\(N\)个点\(M\)条边的无向图,要用\(C\)种颜色去给每个顶点染色. 如果一种染色方案可以旋转得到另一种染色方案,那么说明这两种染色方案是等价的. 求所有染色方案数 \( ...

  2. dubbo rpc filter实现剖析(二)

    2.6.3版本,之前读的是2.4.9版本 本篇主要阐述dubbo rpc的filter的实现,包括作用,用法,原理,与Spring Cloud在这些能力的对比. 整个filter列表的获取过程在 co ...

  3. Model View Controller(MVC) in PHP

    The model view controller pattern is the most used pattern for today’s world web applications. It ha ...

  4. 【java基础 16】抽象类和接口的区别

    导读:前两天闲着没事儿,看了本书,然后写了点代码,在接口里面写了默认方法实现,因为书上说这个特性是从java8开始的,我还特地给测了一下java7. 没过几天,就有一个技术分享会,刚好也是讲java8 ...

  5. poj2135最小费用最大流经典模板题

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13509   Accepted: 5125 Descri ...

  6. 【转】Building a RESTful Web Service

    目标 构建一个service,接收如下HTTP GET请求: [plain] view plain copy   http://localhost:8080/greeting 并返回如下JSON格式的 ...

  7. Struts2的文件上传与下载

    <一>简述: Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUp ...

  8. R语言处理1975-2011年的人口信息

    1975-2011年的数据中. 1)分别统计每年人口最多的国家是哪个?有多少 2)统计出各个国家的1975-2011年的平均人口增长率 3)统计每年人口最多的十个国家 4)统计出每年人口最少的十个国家 ...

  9. 【bzoj1954】Pku3764 The xor-longest Path Trie树

    题目描述  给定一棵n个点的带权树,求树上最长的异或和路径 输入 The input contains several test cases. The first line of each test ...

  10. 大杂烩 Classpath / Build path / Debug关联源码 / JDK&JRE区别

    Classpath的理解及其使用方式 原文地址:http://blog.csdn.net/wk1134314305/article/details/77940147?from=bdhd_site 摘要 ...