关于Android的EditText使用自定义键盘的问题

j249050279 2013-06-24 11:43:23
小弟最近需要做一个自定义键盘,在网上搜了一些资料。使用android.inputmethodservice.KeyboardView这个东西来实现自定义键盘 遇到了几个小问题 求各位大神们来解答一下。




keyboardView.setKeyboard(new Keyboard(this, R.xml.qwerty));
keyboardView.setEnabled(true);
keyboardView.setPreviewEnabled(true);

edt_text.setOnTouchListener(new OnTouchListener()
{

public boolean onTouch(View v, MotionEvent event)
{
int inputType = edt_text.getInputType();
edt_text.setInputType(InputType.TYPE_NULL);// 让系统键盘不弹出
showKeyboard();
edt_text.setInputType(inputType);
return false;
}
});

keyboardView.setOnKeyboardActionListener(new OnKeyboardActionListener()
{
public void onKey(int primaryCode, int[] keyCodes)
{
Editable editable = edt_text.getText();
int start = edt_text.getSelectionStart();
if (primaryCode == Keyboard.KEYCODE_CANCEL)
{
hideKeyboard();
}
else if (primaryCode == Keyboard.KEYCODE_DELETE)
{
if (editable != null && editable.length() > 0)
{
editable.delete(start - 1, start);
}
}
else
{
editable.insert(start, Character.toString((char) primaryCode));
}

}

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

}

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

}

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

}

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

}

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

}

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

}

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

}

});
private void showKeyboard()
{
int visibility = keyboardView.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE)
{
keyboardView.setVisibility(View.VISIBLE);
System.out.println("showKeyboard");
}
}

private void hideKeyboard()
{
int visibility = keyboardView.getVisibility();
if (visibility == View.VISIBLE)
{
keyboardView.setVisibility(View.INVISIBLE);
System.out.println("hideKeyboard");
}
}

Activity布局:

<LinearLayout 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"
android:orientation="vertical" >

<EditText
android:id="@+id/edt_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/keyboard" />

<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:focusable="true"
android:focusableInTouchMode="true"
android:visibility="gone" />
</RelativeLayout>

</LinearLayout>

自定义键盘布局:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="33%p"
android:horizontalGap="0px"
android:verticalGap="0px"
android:keyHeight="46dip">
<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="57419" -->
<!-- android:keyEdgeFlags="right" -->
<!-- android:keyIcon="@drawable/sym_keyboard_left" /> -->
</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="57421" -->
<!-- android:keyEdgeFlags="right" -->
<!-- android:keyIcon="@drawable/sym_keyboard_right" /> -->
</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:keyHeight="92dip" -->
<!-- android:keyEdgeFlags="right" -->
<!-- android:isRepeatable="true" 两列 -->
<!-- android:keyLabel="完成" /> -->
</Row>
<Row>
<Key android:codes="42" android:keyLabel="*" />
<Key android:codes="48" android:keyLabel="0" />
<Key android:codes="35" android:keyLabel="#" />
<!-- <Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete" /> -->
</Row>
</Keyboard>

...全文
6052 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
王子小沫 2015-11-25
  • 打赏
  • 举报
回复
点击弹框框的效果不想要怎么去掉啊 亲们 ~
CJ_小叮咚 2015-11-20
  • 打赏
  • 举报
回复
求解~~问题二有解决方法吗?
lanbooli 2015-09-18
  • 打赏
  • 举报
回复
同问,第二个问题还是不明白
rost270538523 2015-01-13
  • 打赏
  • 举报
回复
问题二有更好的解决方法没有?
yonghengprince2 2014-10-20
  • 打赏
  • 举报
回复
setInputType(InputType.TYPE_NULL); 关标没了,不能用这个啊
s2221219 2014-02-07
  • 打赏
  • 举报
回复
if (android.os.Build.VERSION.SDK_INT <= 10) { edit.setInputType(InputType.TYPE_NULL); } else { getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); try { Class<EditText> cls = EditText.class; Method setShowSoftInputOnFocus; setShowSoftInputOnFocus = cls.getMethod( "setShowSoftInputOnFocus", boolean.class); setShowSoftInputOnFocus.setAccessible(true); setShowSoftInputOnFocus.invoke(edit, false); } catch (Exception e) { e.printStackTrace(); } } 这个更好点
whxceg 2014-01-31
  • 打赏
  • 举报
回复
正是我想要的谢谢了楼主
sryhaoqq 2013-12-16
  • 打赏
  • 举报
回复
楼主这个对我太有帮助了。
j249050279 2013-06-25
  • 打赏
  • 举报
回复
第一个问题也搞定了 要区分一下版本
j249050279 2013-06-25
  • 打赏
  • 举报
回复
第二个问题已经解决咯 由于清单文件的Application节点上建项目时默认加上了android:theme="@style/AppTheme" 所以白色的字白色的背景 看不到。 话说第一个光标的问题该怎么解决?
j249050279 2013-06-25
  • 打赏
  • 举报
回复
引用 4 楼 yuanjiangxujin 的回复:
http://download.csdn.net/detail/yuanjiangxujin/5641095
不一样吧 而且他的也有点问题的 光标也不会随你点击哪里就在哪里显示
j249050279 2013-06-24
  • 打赏
  • 举报
回复
怎么没人来解答啊。。。
yuanjiangxujin 2013-06-24
  • 打赏
  • 举报
回复
http://download.csdn.net/detail/yuanjiangxujin/5641095
j249050279 2013-06-24
  • 打赏
  • 举报
回复
引用 2 楼 dengliulin 的回复:
引用 1 楼 j249050279 的回复:
怎么没人来解答啊。。。
高手不会来这里的。你没看到一堆问号?提问帖的恢复寥寥无几,有资源了就全来了。。。 哈哈 还是百度好
月出惊弓鸟 2013-06-24
  • 打赏
  • 举报
回复
引用 1 楼 j249050279 的回复:
怎么没人来解答啊。。。
高手不会来这里的。你没看到一堆问号?提问帖的恢复寥寥无几,有资源了就全来了。。。 哈哈 还是百度好

80,361

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧