您的位置:首页 > 移动开发 > Android开发

android 之自定义键盘 实现动态效果

2014-08-24 15:18 435 查看
第一步:自定义 键盘组件:KeyboardView是google官方自己提供的键盘组件

http://www.baidu.com/link?url=y_iCppnsVfwmUMOZ-o_9wg2Yb3XRrzZ6FmDQfvZ-S2FctZPKkv9HUz5gvMnc_oDTN_w2vz9qMd8dmprhgqEBhR2aXmoZwb-QAFuu-H7vae889mTGncbEudBn1HksvRYT

public class CustomKeyboardView extends KeyboardView {

public CustomKeyboardView(Context context, AttributeSet attrs) {

super(context, attrs);

}

//设置键盘从下往上实现的缓慢的动态效果,一般的键盘是直接跳出来,继承了 KeyboardView 也主要是为了自定义

public void showWithAnimation(Animation animation) {

animation.setAnimationListener(new AnimationListener() {

@Override

public void onAnimationStart(Animation animation) {

// TODO Auto-generated method stub

}

@Override

public void onAnimationRepeat(Animation animation) {

// TODO Auto-generated method stub

}

@Override

public void onAnimationEnd(Animation animation) {

setVisibility(View.VISIBLE);

}

});

setAnimation(animation);

}

}

第二步:xml文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/linpass"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="输入密码:"

android:textColor="@color/Black"

android:textSize="25dp" />

<EditText

android:id="@+id/editfirst"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:ems="10"

android:singleLine="true" />

</LinearLayout>

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="重复密码:"

android:textColor="@color/Black"

android:textSize="25dp" />

<EditText

android:id="@+id/editsce"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:layout_weight="1"

android:ems="10"

android:inputType="textPassword"

android:singleLine="true" >

</EditText>

</LinearLayout>

<RelativeLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="@drawable/btn_input_press_1" >

<com.scu.diary.view.CustomKeyboardView //public class CustomKeyboardView extends KeyboardView {

android:id="@+id/keyboard_view"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:background="@drawable/btn_input_normal"

android:focusable="true"

android:focusableInTouchMode="true"

android:keyBackground="@drawable/btn_keyboard_key"

android:keyPreviewLayout="@layout/textview"

android:keyTextColor="@color/Black"

android:keyTextSize="20dp"

android:visibility="gone" />

</RelativeLayout>

</LinearLayout>

第三部:已定义一个KeyUtil

public class KeyboardUtil {

private Context context;

private Activity activity;

private CustomKeyboardView keyboardView;

private Keyboard ketword;// 字母键盘

private Keyboard keynum, keypun;// 数字键盘

public boolean isnun = false;// 是否数据键盘

public boolean isupper = false;// 是否大写

private EditText ed;

public KeyboardUtil(Activity activity, Context context, EditText edit) {

this.activity = activity;

this.context = context;

this.ed = edit;

//这三个分别是标点符号还有 字母等,是一个存xml文件,

ketword = new Keyboard(context, R.xml.qwerty);

keynum = new Keyboard(context, R.xml.symbols);

keypun = new Keyboard(context, R.xml.punctuation);

keyboardView = (CustomKeyboardView) activity.findViewById(R.id.keyboard_view);

keyboardView.setKeyboard(ketword); //设置默认键盘

keyboardView.setEnabled(true); //设置是否可以实现按键

keyboardView.setPreviewEnabled(true); //设置按键后跳出来的放大键

keyboardView.setOnKeyboardActionListener(listener);

}

private OnKeyboardActionListener listener = new OnKeyboardActionListener() {

@Override

public void swipeUp() {

}

@Override

public void swipeRight() {

}

@Override

public void swipeLeft() {

}

@Override

public void swipeDown() {

}

@Override

public void onText(CharSequence text) {

}

@Override

public void onRelease(int primaryCode) {

}

@Override

public void onPress(int primaryCode) {

}

//最主要的就是实现这一个方法,他是返回按键的值,可以在这里对按键的值进行响应

@Override

public void onKey(int primaryCode, int[] keyCodes) {

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(ketword);

} /*

* else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 数字键盘切换 //这是官方文档自己内嵌的键值

* if (isnun) { isnun = false; keyboardView.setKeyboard(k1); } else

* { isnun = true; keyboardView.setKeyboard(k2); } }

*/else if (primaryCode == 57419) { // go left

if (start > 0) {

ed.setSelection(start - 1);

}

} else if (primaryCode == 57000) {
// go left //这个数字是可以自己定义的

keyboardView.setKeyboard(keynum);

} else if (primaryCode == 57001) { // go left

keyboardView.setKeyboard(ketword);

} else if (primaryCode == 57002) { // go left

keyboardView.setKeyboard(keypun);

}

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> keylist = ketword.getKeys();

if (isupper) {// 大写切换小写

isupper = false;

for (Key key : keylist) {

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 : keylist) {

if (key.label != null && isword(key.label.toString())) {

key.label = key.label.toString().toUpperCase();

key.codes[0] = key.codes[0] - 32;

}

}

}

}

//这个就是用于回调当单击了 edittext的时候进行的响应

/*public boolean onTouch(View v, MotionEvent event) {

int type=editsce.getInputType();

editsce.setInputType(InputType.TYPE_NULL);

new KeyboardUtil(activity, context, editsce).showKeyboard();

//editsce.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

editsce.setInputType(type);

return false;

}*/

//显示的时候加载动画,这个可以自己去定义动画的效果

public void showKeyboard() {

int visibility = keyboardView.getVisibility();

if (visibility == View.GONE || visibility == View.INVISIBLE) {

Animation animation = AnimationUtils.loadAnimation(activity,

R.anim.slide_in_bottom);

keyboardView.showWithAnimation(animation);

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;

}

}

第四部:主文件中对edittext单击时候的响应

editsce.setOnTouchListener(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

int type=editsce.getInputType();

editsce.setInputType(InputType.TYPE_NULL);

new KeyboardUtil(activity, context, editsce).showKeyboard();

//editsce.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

editsce.setInputType(type);

return false;

}

});

editfirst.setOnTouchListener(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

// int inputback = editfirst.getInputType();

editfirst.setInputType(InputType.TYPE_NULL);

new KeyboardUtil(activity, context, editfirst).showKeyboard();

// editfirst.setInputType(inputback);

return false;

}

});

第五步:字符键盘的定义:

<?xml version="1.0" encoding="UTF-8"?>

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"

android:horizontalGap="0.0px"

android:keyHeight="@dimen/key_height"

android:keyWidth="10.000002%p"

android:verticalGap="0.0px" >

<Row>

<Key

android:codes="113"

android:keyEdgeFlags="left"

android:keyLabel="q" />

<Key

android:codes="119"

android:keyLabel="w" />

<Key

android:codes="101"

android:keyLabel="e" />

<Key

android:codes="114"

android:keyLabel="r" />

<Key

android:codes="116"

android:keyLabel="t" />

<Key

android:codes="121"

android:keyLabel="y" />

<Key

android:codes="117"

android:keyLabel="u" />

<Key

android:codes="105"

android:keyLabel="i" />

<Key

android:codes="111"

android:keyLabel="o" />

<Key

android:codes="112"

android:keyEdgeFlags="right"

android:keyLabel="p" />

</Row>

<!-- <Row>

<Key

android:codes="113"

android:keyEdgeFlags="left"

android:keyHeight="5dip"

/>

</Row> -->

<Row>

<Key

android:codes="97"

android:horizontalGap="4.999995%p"

android:keyEdgeFlags="left"

android:keyLabel="a" />

<Key

android:codes="115"

android:keyLabel="s" />

<Key

android:codes="100"

android:keyLabel="d" />

<Key

android:codes="102"

android:keyLabel="f" />

<Key

android:codes="103"

android:keyLabel="g" />

<Key

android:codes="104"

android:keyLabel="h" />

<Key

android:codes="106"

android:keyLabel="j" />

<Key

android:codes="107"

android:keyLabel="k" />

<Key

android:codes="108"

android:keyEdgeFlags="right"

android:keyLabel="l" />

</Row>

<!-- <Row>

<Key

android:codes="113"

android:keyEdgeFlags="left"

android:keyHeight="5dip"

/>

</Row> -->

<Row>

<Key

android:codes="-1"

android:isModifier="true"

android:isSticky="true"

android:keyEdgeFlags="left"

android:keyIcon="@drawable/sym_keyboard_shift"

android:keyWidth="14.999998%p" />

<Key

android:codes="122"

android:keyLabel="z" />

<Key

android:codes="120"

android:keyLabel="x" />

<Key

android:codes="99"

android:keyLabel="c" />

<Key

android:codes="118"

android:keyLabel="v" />

<Key

android:codes="98"

android:keyLabel="b" />

<Key

android:codes="110"

android:keyLabel="n" />

<Key

android:codes="109"

android:keyLabel="m" />

<Key

android:codes="-5"

android:isRepeatable="true"

android:keyEdgeFlags="right"

android:keyIcon="@drawable/sym_keyboard_delete"

android:keyWidth="14.999998%p" />

</Row>

<Row android:rowEdgeFlags="bottom" >

<Key

android:codes="57000"

android:keyLabel="123"

android:keyWidth="20.000004%p" />

<Key

android:codes="57002"

android:keyLabel="符"

android:keyWidth="20.000004%p"

/>

<Key

android:codes="44"

android:keyLabel=","

android:keyWidth="9.999998%p" />

<Key

android:codes="32"

android:isRepeatable="true"

android:keyIcon="@drawable/sym_keyboard_space"

android:keyWidth="20.999996%p" />

<Key

android:codes="46"

android:keyLabel="."

android:keyWidth="9.999998%p" />

<Key

android:codes="-3"

android:keyEdgeFlags="right"

android:keyLabel="完成"

android:keyWidth="20.000004%p" />

</Row>

</Keyboard>

<?xml version="1.0" encoding="utf-8"?>

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"

android:horizontalGap="0px"

android:keyHeight="@dimen/key_height"

android:keyWidth="25%p"

android:verticalGap="0px" >

<Row>

<Key

android:codes="49"

android:keyLabel="1" />

<Key

android:codes="50"

android:keyLabel="2" />

<Key

android:codes="51"

android:keyLabel="3" />

<Key

android:codes="-5"

android:keyEdgeFlags="right"

android:keyIcon="@drawable/sym_keyboard_delete" />

</Row>

<Row>

<Key

android:codes="52"

android:keyLabel="4" />

<Key

android:codes="53"

android:keyLabel="5" />

<Key

android:codes="54"

android:keyLabel="6" />

<Key

android:codes="57002"

android:keyEdgeFlags="right"

android:keyLabel="符"

/>

</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="-3"

android:isRepeatable="true"

android:keyEdgeFlags="right"

android:keyHeight="100dip"

android:keyLabel="完成" />

</Row>

<Row>

<Key

android:codes="57001"

android:keyLabel="ABC" />

<Key

android:codes="48"

android:keyLabel="0" />

<Key

android:codes="32"

android:isRepeatable="true"

android:keyIcon="@drawable/sym_keyboard_space"

/>

</Row>

</Keyboard>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: