Customized keyboard
I am very new to developping Android application and I am facing a difficulty.
What I want to do is to use a specific keyboard when I click on开发者_StackOverflow社区 an
EditText
. So far, I have found the Keyboard
and KeyboardView
classes but I haven't succeeded to do what I want yet.
Here is the description of where I am :
- I have described my keyboard in a XML file,
- I create a "KeyboardView" object,
- I initialize it with
clavier=new KeyboardView(activité, (AttributeSet)findViewById(R.xml.clavier_numerique));
- but I don't know how to replace the standard keyboard with this customized keyboard.
Am I doing something wrong? What else should I do?
Thanks in advance for the time you will spend trying to help me.
You should use something like this:
//retrieve the keyboard view from xml
kbdV= (KeyboardView) findViewById(R.id.kbd);
//set the keyboard layout to the layout you defined in res/xml/keyboard_layout.xml
kbdV.setKeyboard(new Keyboard(this,R.xml.keyboard_layout)); //defines the keyboard layout
//add a keyboard action listener
kbdV.setOnKeyboardActionListener(new KeyboardView.OnKeyboardActionListener(){
public void onKey(int primaryCode, int[] keyCodes) {
handlePress(primaryCode, keyCodes); // callback to handle keypresses
}
public void onPress(int primaryCode) {}
public void onRelease(int primaryCode) {}
public void onText(CharSequence text) {}
public void swipeDown() {}
public void swipeLeft() {}
public void swipeRight() {}
public void swipeUp() {}
});
with a layout xml file similar to this :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- your widgets here -->
<KeyboardView android:id="@+id/kbd" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</LinearLayout>
At first you should decide what you want from keyboard:
if you just want to change to numbers you can do that by the first answer from Macarse
if you want a complete customized keyboard you should use Keyboard
and KeyboardView
classes by a second project
You need to specify an inputType
in the xml:
<EditText android:inputType="textUri"/>
or from the code doing:
EditText input;
input.setInputType(InputType.TYPE_CLASS_NUMBER);
You can read the available inputType
s here.
When you initialize it with clavier=new KeyboardView(activity, (AttributeSet)findViewById(R.xml.clavier_numerique),EditText edit); You can transfer the EditText object in. And hide the standard keyboard,show the customized KeyboardView like this.
public void showKeyboard() {
if (edit != null) {
InputMethodManager imm = (InputMethodManager)mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(), 0);
}
keyboardView.setVisibility(View.VISIBLE);
keyboardView.setEnabled(true);
}
精彩评论