1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android开发自定义键盘 如何制作Android自定义键盘?

android开发自定义键盘 如何制作Android自定义键盘?

时间:2021-07-30 11:05:16

相关推荐

android开发自定义键盘 如何制作Android自定义键盘?

首先你需要一个keyboard.xml文件,该文件将放置在res/xml文件夹(如果文件夹不存在,则创建该文件夹)。<?xml version="1.0"encoding="utf-8"?>

android:keyWidth="15%p"

android:keyHeight="15%p">

*注意,您必须创建backspace可绘制,并将其放置在Res/draable-ldpi文件夹中,其大小非常小(比如18x18像素)。

然后,在您希望使用它的XML文件中(您的TextView在其中),您应该添加以下代码:

...>

.....

android:id="@+id/keyboardview"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_centerHorizontal="true"

android:focusable="true"

android:focusableInTouchMode="true"

android:visibility="gone"

/>

......

*注意,您将放置android.inputmethodservice.KeyboardView在,必须是RelativeLayout以便能够将alignParentBottom="true"(通常键盘在屏幕底部显示)

然后,您需要在onCreate的功能Activity处理TextView您想要将键盘附加到//CreatetheKeyboard

mKeyboard=newKeyboard(this,R.xml.keyboard);

//LookuptheKeyboardView

mKeyboardView=(KeyboardView)findViewById(R.id.keyboardview);

//Attachthekeyboardtotheview

mKeyboardView.setKeyboard(mKeyboard);

//Donotshowthepreviewballoons

//mKeyboardView.setPreviewEnabled(false);

//Installthekeyhandler

mKeyboardView.setOnKeyboardActionListener(mOnKeyboardActionListener);

*注意mKeyboard和mKeyboardView是您必须创建的私有类变量。

然后,您需要以下函数来打开键盘(您必须通过onClickXML属性)publicvoidopenKeyboard(Viewv)

{

mKeyboardView.setVisibility(View.VISIBLE);

mKeyboardView.setEnabled(true);

if(v!=null)((InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(),0);

}

最后你需要OnKeyboardActionListener它将处理你的事件privateOnKeyboardActionListenermOnKeyboardActionListener=newOnKeyboardActionListener(){

@OverridepublicvoidonKey(intprimaryCode,int[]keyCodes)

{

//HerechecktheprimaryCodetoseewhichkeyispressed

//basedontheandroid:codesproperty

if(primaryCode==1)

{

Log.i("Key","Youjustpressed1button");

}

}

@OverridepublicvoidonPress(intarg0){

}

@OverridepublicvoidonRelease(intprimaryCode){

}

@OverridepublicvoidonText(CharSequencetext){

}

@OverridepublicvoidswipeDown(){

}

@OverridepublicvoidswipeLeft(){

}

@OverridepublicvoidswipeRight(){

}

@OverridepublicvoidswipeUp(){

}};

希望这有帮助!

发现的大部分代码这里

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。