android自定义数字软键盘、修改review按钮的样式

来源:互联网 发布:python 获取期货数据 编辑:程序博客网 时间:2024/04/29 03:25

Android提供KeyboardView可以让用户实现对软键盘的自定义;具体的定义数字键盘,还是字符键盘可以按照自己的需求进行定制;

本文提供数字键盘的自定义实现,并解决软键盘按下后review背景按钮样式的修改;

(1)main.xml文件下面定义一个KeyboardView;

(2)在xml文件夹下面定义一个数字键盘number.xml文件;

(3)提供一个软件盘的实现工具类

先上运行后的效果图,不是你要的就不用仔细读代码啦



main.xml中的核心代码如下:

<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="com.lyy.input.MainActivity" >
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="31dp"
        android:ems="10"
        android:inputType="number" >
    </EditText>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <android.inputmethodservice.KeyboardView
            android:id="@+id/keyboard_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="#F4F4F4"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:keyBackground="@drawable/btn_keyboard_key"
            android:keyPreviewLayout="@layout/key_preview_layout"
            android:shadowColor="#FFFFFF"
            android:shadowRadius="0.0"
            android:keyTextColor="#000000" />
    </RelativeLayout>
</RelativeLayout>


number.xml中的代码如下;主要用来定义数字键;注意该文件存放在xml文件夹下面;

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalGap="3dp"
    android:keyHeight="48dp"
    android:keyWidth="33.33%p"
    android:verticalGap="3dp" >

    <Row>
        <Key
            android:codes="49"
            android:keyLabel="1" />
        <Key
            android:codes="50"
            android:keyLabel="2" />
        <Key
            android:codes="51"
            android:keyLabel="3" />
    </Row>
    <Row>
        <Key
            android:codes="52"
            android:keyLabel="4" />
        <Key
            android:codes="53"
            android:keyLabel="5" />
        <Key
            android:codes="54"
            android:keyLabel="6" />
    </Row>
    <Row>
        <Key
            android:codes="55"
            android:keyLabel="7" />
        <Key
            android:codes="56"
            android:keyLabel="8" />
        <Key
            android:codes="57"
            android:keyLabel="9" />
    </Row>
    <Row>
        <Key
            android:codes="-5"
            android:keyIcon="@drawable/sym_keyboard_delete" />
        <Key
            android:codes="48"
            android:keyLabel="0" />
        <Key
            android:codes="-3"
            android:isRepeatable="true"
            android:keyLabel="完成" />
    </Row>
</Keyboard>

软键盘工具类的代码如下:

public class KeyboardUtil {

private Context ctx;
private Activity act;
private KeyboardView keyboardView;   //keyBoardView组建
private Keyboard k1, k2;// 数字键盘, 字母键盘
public boolean isnun = true;// 是否数据键盘
public boolean isupper = false;// 是否大写
private EditText ed;

public KeyboardUtil(Activity act, Context ctx, EditText edit) {
this.act = act;
this.ctx = ctx;
this.ed = edit;

k1 = new Keyboard(ctx, R.xml.numbers);
k2 = new Keyboard(ctx, R.xml.text);
keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);
keyboardView.setKeyboard(k1);
keyboardView.setEnabled(true);
keyboardView.setPreviewEnabled(true);
keyboardView.setOnKeyboardActionListener(listener);
}


private OnKeyboardActionListener listener = new OnKeyboardActionListener() {


@Override
public void swipeUp() {
// TODO Auto-generated method stub
}


@Override
public void swipeRight() {
// TODO Auto-generated method stub
}


@Override
public void swipeLeft() {
// TODO Auto-generated method stub
}


@Override
public void swipeDown() {
// TODO Auto-generated method stub
}


@Override
public void onText(CharSequence text) {
// TODO Auto-generated method stub
}


@Override
public void onRelease(int primaryCode) {
// TODO Auto-generated method stub
}


@Override
public void onPress(int primaryCode) {
// TODO Auto-generated method stub
}

@Override
public void onKey(int primaryCode, int[] keyCodes) {
// TODO Auto-generated method stub
Editable editable = ed.getText();
int start = ed.getSelectionStart();
if (primaryCode == Keyboard.KEYCODE_CANCEL) // 完成
{
hideKeyboard();
} else if (primaryCode == Keyboard.KEYCODE_DELETE) {
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
}
}
} else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小写切换
changeKey();
keyboardView.setKeyboard(k2);


} else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 数字键盘切换
if (isnun) {
isnun = false;
keyboardView.setKeyboard(k2);
} else {
isnun = true;
keyboardView.setKeyboard(k1);
}
} else if (primaryCode == 57419) { // go left
if (start > 0) {
ed.setSelection(start - 1);
}
} else if (primaryCode == 57421) { // go right
if (start < ed.length()) {
ed.setSelection(start + 1);
}
} else {
editable.insert(start, Character.toString((char) primaryCode));
}
}
};


/**
* 键盘大小写切换
*/
private void changeKey() {
List<Key> list = k2.getKeys();
if (isupper) {
isupper = false;
for (Key key : list) {
if (key.label != null && isword(key.label.toString())) {
key.label = key.label.toString().toLowerCase();
key.codes[0] = key.codes[0] + 32;
}
}
} else {
isupper = true;
for (Key key : list) {
if (key.label != null && isword(key.label.toString())) {
key.label = key.label.toString().toUpperCase();
key.codes[0] = key.codes[0] - 32;
}
}
}
}

public void showKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE) {
keyboardView.setVisibility(View.VISIBLE);
}
}

public void hideKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.VISIBLE) {
keyboardView.setVisibility(View.INVISIBLE);
}
}

private boolean isword(String str) {
String wordstr = "abcdefghijklmnopqrstuvwxyz";
if (wordstr.indexOf(str.toLowerCase()) > -1) {
return true;
}
return false;
}
}


MainActivity.java中的核心代码如下:

private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText1);
editText.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int numberType = editText.getInputType();
editText.setInputType(InputType.TYPE_NULL);
new KeyboardUtil(MainActivity.this, MainActivity.this, editText)
.showKeyboard();
editText.setInputType(numberType);

return true;
}
});
}

注意,下面说一下如何修改键盘按下后,Toast按键框的样式

KeyBoardView下面的这行代码:android:keyPreviewLayout="@layout/key_preview_layout" 

keyPreviewLayout就是用来定义 按键按下宏,提示用户按下的键提醒框样式;这个属性我翻阅文档好久才发现的;恭喜那些看文章看到最后的亲们,你们定义的键盘样式一定不会太挫;

贴一下key_preview_layout下面的代码:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:gravity="center"
    android:text=""
    android:textColor="@android:color/black"
    android:textSize="20sp" />

该文件下面的android:background, android:textColor 就是控制 按键提醒框中的背景和文字的样式,亲你懂的;一个满意的自定义数字键盘大功告成啦!

如需工程源代码请联系:qq:1169931878(江南怡园)

3 0
原创粉丝点击