使用自定义键盘一个EditText [英] Use an EditText with custom keyboard
问题描述
我是新来的机器人。我想创建一个自定义键盘的应用程序,仅用于应用程序。不是一般的键盘分布。基本上我的测试是有这样一个布局:
I am new to android. I am trying to create an application with a custom keyboard, used for the application only. Not a general keyboard for distribution. Basically my test is to have a layout like:
Relative Layout
Relative Layout id tolayouot
EditText id userID
Button
LinearLayout alignbottomparent=TRUE
InputMethodService.KeyboardView id kv
因此,有与一个编辑框和一个按钮和底部的顶部是键盘。当我preSS按钮,出现自定义键盘。然后我想能够输入到的EditText框。
So there is a top section with an Edit Box and a button and the bottom section is the keyboard. When I press the button, the Custom Keyboard appears. Then I would like to be able to type into the EditText box.
我已经做了私有变量来实现keyboardactionlistener。它基本上遵循和榜样,只把日志消息了。
I have made a private variable to implement keyboardactionlistener. It basically follows and example and only puts Log messages out.
private OnKeyboardActionListener mykal = new OnKeyboardActionListener()
{
@Override
public void swipeUp() {
Log.d(TAG, "swipeUp");
}
@Override
public void swipeRight() {
Log.d(TAG, "swipeRight");
}
@Override
public void swipeLeft() {
Log.d(TAG, "swipeLeft");
}
@Override
public void swipeDown() {
Log.d(TAG, "swipeDown");
}
@Override
public void onText(CharSequence text) {
Log.d(TAG, "onText? \"" + text + "\"");
}
@Override
public void onRelease(int primaryCode) {
Log.d(TAG, "onRelease? primaryCode=" + primaryCode);
}
@Override
public void onPress(int primaryCode) {
Log.d(TAG, "onPress? primaryCode=" + primaryCode);
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
Log.d(TAG, "onKey? primaryCode=" + primaryCode);
int n1 = 0; // -1 count
for (int keyCode : keyCodes) {
if (keyCode == -1) {
n1++;
continue;
}
Log.v(TAG, "keyCode=" + keyCode);
}
Log.v(TAG, "keyCode=-1 *" + n1);
}
};
}
下面是上创建code
Here is the on create code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_example);
Button b = (Button) this.findViewById(R.id.tstbtn);
myedit = (EditText) this.findViewById(R.id.user_ID);
rl = (RelativeLayout) this.findViewById(R.id.toplayout);
b.setOnClickListener(clistner);
KeyboardView keyboardView = (KeyboardView) findViewById(R.id.keyboardView);
Keyboard keyboard = new Keyboard(this, R.xml.tstkbd);
keyboardView.setKeyboard(keyboard);
keyboardView.setEnabled(true);
keyboardView.setPreviewEnabled(true);
myedit.setOnTouchListener(mytouch);
// keyboardView.setOnKeyListener(this);
keyboardView.setOnKeyboardActionListener(mykal);
//hide the default keyboard
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
所以,当我跑我的测试中,默认键盘就会消失,就像我想,我点击按钮,就会出现我的键盘。所有在keyboardactionlistener日志消息在调试器显示出来,当我preSS的按键,使键盘工作。我可以将焦点设置到的EditText,但如果我点击我的自定义键盘上的键,我没有得到在盒子的EditText任何文本。
So when I run my test, the default keyboard disappears just like I want, I click the button and my keyboard appears. All of the log messages in the keyboardactionlistener show up in the debugger when I press the keys, so the keyboard is working. I can set the focus to the EditText but if I click a key on my custom keyboard, I do not get any text in the EditText box.
有没有办法顺利获得自定义键盘按键presses在盒子的EditText注册?
Is there a way to smoothly get the keypresses from the custom keyboard to register in the EditText box?
我知道我可以使用KeyBoardactionListener的安其事件来操纵的EditText框中的文本,但我想用一个固有的功能。
I know I can use the OnKey Event of the KeyBoardactionListener to manipulate the text in the EditText box but I would like to use an intrinsic function.
我曾尝试:
- 发送的事件消息的EditText并向RelativeLayout的toprl但坠毁。
- 设置为一个的EditText onTouchListener。
是实现这个创建InputMethodService的唯一途径?
Is the only way to accomplish this to create an InputMethodService?
任何人都可以请解释为什么这不工作?
Can anyone please explain why this doesnt work???
下面的两个例子是那些我试图用
The two examples below were the ones I was trying to use.
HTTP://androidpadanam.word$p$pss .COM / 2013/05/29 / customkeyboard,例如/
http://www.infiniterecursion.us/2011/02/android -activity定制-keyboard.html
推荐答案
在没有任何理由在显示的EditText是因为你没有做的任何事件,当您收到他们。你必须应对监听事件和实施重点preSS自己。下面是你如何执行的动作来添加文本的例子。
The reason nothing is showing in the EditText is because you're not doing anything with the events when you receive them. You have to respond to the listener events and implement the key press yourself. Below is an example of how you could implement an action to add text.
@Override
public void onRelease(int primaryCode) {
Log.d(TAG, "onRelease? primaryCode=" + primaryCode);
myedit.setText(myedit.getText().toString() + getKeyForPrimaryCode(primaryCode));
}
请注意:getKeyForPrimary code也只是你犯了一个方法,它的主要code转换的关键
Note: getKeyForPrimaryCode would just be a method you make which converts the primary code to the label on the key.
这篇关于使用自定义键盘一个EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!