How can you make a custom keyboard in Android?

I want to make my own keyboard. I do not know how to do this using XML and Java. The next picture is the keyboard model I want to make. Only numbers are needed.

enter image description here

+80
android keyboard
Mar 06 2018-12-12T00:
source share
9 answers

First of all, you need a keyboard.xml file that will be placed in the res/xml folder (if the folder does not exist, create it).

 <?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyWidth="15%p" android:keyHeight="15%p" > <Row> <Key android:codes="1" android:keyLabel="1" android:horizontalGap="4%p"/> <Key android:codes="2" android:keyLabel="2" android:horizontalGap="4%p"/> <Key android:codes="3" android:keyLabel="3" android:horizontalGap="4%p" /> <Key android:codes="4" android:keyLabel="4" android:horizontalGap="4%p" /> <Key android:codes="5" android:keyLabel="5" android:horizontalGap="4%p" /> </Row> <Row> <Key android:codes="6" android:keyLabel="6" android:horizontalGap="4%p"/> <Key android:codes="7" android:keyLabel="7" android:horizontalGap="4%p"/> <Key android:codes="8" android:keyLabel="8" android:horizontalGap="4%p" /> <Key android:codes="9" android:keyLabel="9" android:horizontalGap="4%p" /> <Key android:codes="0" android:keyLabel="0" android:horizontalGap="4%p" /> </Row> <Row> <Key android:codes="-1" android:keyIcon="@drawable/backspace" android:keyWidth="34%p" android:horizontalGap="4%p"/> <Key android:codes="100" android:keyLabel="Enter" android:keyWidth="53%p" android:horizontalGap="4%p"/> </Row> </Keyboard> 

** Please note that you will have to create a backspace hood and place it in the RES / hood-ldpi folder with a very small size (for example, 18x18 pixels)

Then, in the xml file you want to use (where your TextView is located), you should add the following code:

 <RelativeLayout ... > ..... <android.inputmethodservice.KeyboardView 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" /> ...... </RelativeLayout> 

** Please note that the xml file in which you place android.inputmethodservice.KeyboardView must be RelativeLayout so that alignParentBottom="true" can be set (usually the keyboards are displayed at the bottom of the screen)

Then you need to add the following code to the onCreate function in the Activity which processes the TextView to which you want to connect the keyboard.

  // Create the Keyboard mKeyboard= new Keyboard(this,R.xml.keyboard); // Lookup the KeyboardView mKeyboardView= (KeyboardView)findViewById(R.id.keyboardview); // Attach the keyboard to the view mKeyboardView.setKeyboard( mKeyboard ); // Do not show the preview balloons //mKeyboardView.setPreviewEnabled(false); // Install the key handler mKeyboardView.setOnKeyboardActionListener(mOnKeyboardActionListener); 

** Please note that mKeyboard and mKeyboardView are private class variables that you must create.

Then you need the following function to open the keyboard (you must associate it with the TextView via the onClick xml property)

  public void openKeyboard(View v) { mKeyboardView.setVisibility(View.VISIBLE); mKeyboardView.setEnabled(true); if( v!=null)((InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0); } 

And finally, you need an OnKeyboardActionListener that will handle your events

 private OnKeyboardActionListener mOnKeyboardActionListener = new OnKeyboardActionListener() { @Override public void onKey(int primaryCode, int[] keyCodes) { //Here check the primaryCode to see which key is pressed //based on the android:codes property if(primaryCode==1) { Log.i("Key","You just pressed 1 button"); } } @Override public void onPress(int arg0) { } @Override public void onRelease(int primaryCode) { } @Override public void onText(CharSequence text) { } @Override public void swipeDown() { } @Override public void swipeLeft() { } @Override public void swipeRight() { } @Override public void swipeUp() { } }; 

Hope this helps !!!

Most of the code is found here.

+73
Jul 27 '14 at 9:50
source share

System keyboard

This answer describes how to create a custom system keyboard that can be used in any application that the user has installed on their phone. If you want to create a keyboard that will only be used in your application, see my other answer .

The example below will look as follows. You can change it for any keyboard layout.

enter image description here

The following steps show how to create a working, customizable system keyboard. As much as possible, I tried to remove any unnecessary code. If you need other features, in the end I provided links for additional help.

1. Launch a new Android project

I named my project "Custom Keyboard". Call it what you want. There is nothing special here. I will just leave MainActivity and "Hello world!" layout as is.

2. Add layout files

Add the following two files to the res/layout folder of your application:

  • keyboard_view.xml
  • key_preview.xml

keyboard_view.xml

This view is like a container that will hold our keyboard. There is only one keyboard in this example, but you can add other keyboards and swap them in this KeyboardView .

 <?xml version="1.0" encoding="utf-8"?> <android.inputmethodservice.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/keyboard_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:keyPreviewLayout="@layout/key_preview" android:layout_alignParentBottom="true"> </android.inputmethodservice.KeyboardView> 

key_preview.xml

A key preview is a layout that appears when you press a keyboard key. It just shows which key you are pressing (in case your big, thick fingers cover it). This is not a multiple choice popup. To do this, you must check the submission of the candidates .

 <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="@android:color/white" android:textColor="@android:color/black" android:textSize="30sp"> </TextView> 

3. Add XML helper files

Create an xml folder in your res folder. (Right-click res and select New> Directory .)

Then add the following two XML files to it. (Right-click the xml folder and choose New> XML Resource File .)

  • number_pad.xml
  • method.xml

number_pad.xml

This is where it gets more interesting. This Keyboard determines the location of the keys .

 <?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyWidth="20%p" android:horizontalGap="5dp" android:verticalGap="5dp" android:keyHeight="60dp"> <Row> <Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/> <Key android:codes="50" android:keyLabel="2"/> <Key android:codes="51" android:keyLabel="3"/> <Key android:codes="52" android:keyLabel="4"/> <Key android:codes="53" android:keyLabel="5" android:keyEdgeFlags="right"/> </Row> <Row> <Key android:codes="54" android:keyLabel="6" android:keyEdgeFlags="left"/> <Key android:codes="55" android:keyLabel="7"/> <Key android:codes="56" android:keyLabel="8"/> <Key android:codes="57" android:keyLabel="9"/> <Key android:codes="48" android:keyLabel="0" android:keyEdgeFlags="right"/> </Row> <Row> <Key android:codes="-5" android:keyLabel="DELETE" android:keyWidth="40%p" android:keyEdgeFlags="left" android:isRepeatable="true"/> <Key android:codes="10" android:keyLabel="ENTER" android:keyWidth="60%p" android:keyEdgeFlags="right"/> </Row> </Keyboard> 

Here are a few things to consider:

  • keyWidth : this is the default width for each key. A value of 20%p means that each key should occupy 20% of the width of the p region. However, you can override it with separate keys, as you can see what happened with the Delete and Enter keys on the third line.
  • keyHeight : this is hard-coded here, but you can use something like @dimen/key_height to set it dynamically for different screen sizes.
  • Gap : The horizontal and vertical clearance shows how much space is left between the keys. Even if you set it to 0px there will still be a small gap.
  • codes : it can be Unicode or a custom code value that determines what happens or what is entered when a key is pressed. See keyOutputText if you want to enter a longer Unicode string.
  • keyLabel : this is the text that is displayed on the key.
  • keyEdgeFlags : indicates on which edge the key should be aligned.
  • isRepeatable : if you hold the key, it will repeat the input.

method.xml

This file tells the system about the available subtypes of the input method. I just included the minimum version here.

 <?xml version="1.0" encoding="utf-8"?> <input-method xmlns:android="http://schemas.android.com/apk/res/android"> <subtype android:imeSubtypeMode="keyboard"/> </input-method> 

4. Add Java code to handle keyboard input.

Create a new Java file. Let it be called MyInputMethodService . This file links everything together. It processes the input received from the keyboard and sends it to any view that receives it (for example, EditText ).

 public class MyInputMethodService extends InputMethodService implements KeyboardView.OnKeyboardActionListener { @Override public View onCreateInputView() { // get the KeyboardView and add our Keyboard layout to it KeyboardView keyboardView = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard_view, null); Keyboard keyboard = new Keyboard(this, R.xml.number_pad); keyboardView.setKeyboard(keyboard); keyboardView.setOnKeyboardActionListener(this); return keyboardView; } @Override public void onKey(int primaryCode, int[] keyCodes) { InputConnection ic = getCurrentInputConnection(); if (ic == null) return; switch (primaryCode) { case Keyboard.KEYCODE_DELETE: CharSequence selectedText = ic.getSelectedText(0); if (TextUtils.isEmpty(selectedText)) { // no selection, so delete previous character ic.deleteSurroundingText(1, 0); } else { // delete the selection ic.commitText("", 1); } break; default: char code = (char) primaryCode; ic.commitText(String.valueOf(code), 1); } } @Override public void onPress(int primaryCode) { } @Override public void onRelease(int primaryCode) { } @Override public void onText(CharSequence text) { } @Override public void swipeLeft() { } @Override public void swipeRight() { } @Override public void swipeDown() { } @Override public void swipeUp() { } } 

Notes:

  • OnKeyboardActionListener listens for keyboard input. It also requires all of these empty methods in this example.
  • InputConnection is what is used to send input to another view, such as EditText .

5. Update the manifest

I put this last, not the first, because it refers to the files that we have already added above. To register a custom keyboard as a system keyboard, you need to add the service section to the AndroidManifest.xml file. Put this in the application section after activity .

 <manifest ...> <application ... > <activity ... > ... </activity> <service android:name=".MyInputMethodService" android:label="Keyboard Display Name" android:permission="android.permission.BIND_INPUT_METHOD"> <intent-filter> <action android:name="android.view.InputMethod"/> </intent-filter> <meta-data android:name="android.view.im" android:resource="@xml/method"/> </service> </application> </manifest> 

It! You should be able to launch your application now. However, you will not see much until you turn on the keyboard in the settings.

6. Turn on the keyboard in the settings

Every user who wants to use your keyboard will need to enable it in the Android settings. For detailed instructions on how to do this, see the following link:

Here is a summary:

  • Go to "Android Settings"> "Languages ​​and input"> "Current keyboard"> "Select keyboards".
  • You should see your custom keyboard in the list. To turn it on.
  • Go back and select Current Keyboard again. You should see your custom keyboard in the list. Choose this.

You should now be able to use the keyboard anywhere you can type in Android.

Further training

You can use a keyboard above, but to create a keyboard that other people want to use, you may have to add more functionality. Check out the links below to find out how.

Goes on

Don't like how the standard KeyboardView looks and behaves? I certainly don’t seem like it has been updated with Android 2.0. What about all these custom keyboards on the Play Store? They don't look like the ugly keyboard above.

The good news is that you can fully customize your own look and feel of the keyboard. You will need to do the following things:

  1. Create your own keyboard view, which subclasses ViewGroup . You can fill it with Button or even create your own custom key views that subclass View . If you use pop-ups, pay attention to this .
  2. Add an event listener user interface to the keyboard. Call its methods for things like onKeyClicked(String text) or onBackspace() .
  3. You do not need to add the keyboard_view.xml , key_preview.xml or number_pad.xml described in the above instructions, since they are all designed for the standard KeyboardView . You will handle all of these aspects of the user interface in your user view.
  4. In your MyInputMethodService class MyInputMethodService implement the custom keyboard listener that you defined in your keyboard class. This is instead of KeyboardView.OnKeyboardActionListener , which is no longer needed.
  5. In your MyInputMethodService onCreateInputView() class, create and return an instance of your custom keyboard. Remember to set the keyboard to order this listener.
+50
Jul 06 '17 at 4:27
source share

Use KeyboardView :

 KeyboardView kbd = new KeyboardView(context); kbd.setKeyboard(new Keyboard(this, R.xml.custom)); kbd.setOnKeyboardActionListener(new OnKeyboardActionListener() { .... } 

now you have kbd , which is a normal view.

The nice thing is that R.xml.custom refers to /res/xml/custom.xml , which defines the keyboard layout in xml. For more information about this file, see Keyboard , Keyboard.Row , Keyboard.Key here .

+30
Aug 12 '12 at 16:20
source share

In-app keyboard

This answer tells you how to make a custom keyboard for use exclusively in your application. If you want to make a system keyboard that can be used in any application, then see my other answer .

An example would look like this. You can change it for any keyboard layout.

enter image description here

1. Launch a new Android project

I named my project InAppKeyboard . Call yours what you want.

2. Add layout files

Keyboard layout

Add a res/layout file to the res/layout folder. I called my keyboard . The keyboard will be a custom composite view that we inflate from this XML layout file. You can use any layout you like to arrange the keys, but I use LinearLayout . Pay attention to the merge tags.

permission / layout / keyboard.xml

 <merge xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="1"/> <Button android:id="@+id/button_2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="2"/> <Button android:id="@+id/button_3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="3"/> <Button android:id="@+id/button_4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="4"/> <Button android:id="@+id/button_5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="5"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="6"/> <Button android:id="@+id/button_7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="7"/> <Button android:id="@+id/button_8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="8"/> <Button android:id="@+id/button_9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="9"/> <Button android:id="@+id/button_0" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="0"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button_delete" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="Delete"/> <Button android:id="@+id/button_enter" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:text="Enter"/> </LinearLayout> </LinearLayout> </merge> 

Activity Layout

For demonstration purposes, our activity has one EditText and the keyboard is at the bottom. I named my custom keyboard view MyKeyboard . (We will add this code in the near future, so for now we will ignore the error.) The advantage of placing all of our code on the keyboard in one view is that it is easy to use in another action or application.

permission / layout / activity_main.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.example.inappkeyboard.MainActivity"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#c9c9f1" android:layout_margin="50dp" android:padding="5dp" android:layout_alignParentTop="true"/> <com.example.inappkeyboard.MyKeyboard android:id="@+id/keyboard" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_alignParentBottom="true"/> </RelativeLayout> 

3. Add Java keyboard file

Add a new Java file. I called my MyKeyboard .

It is important to note here that there is no hard link to any EditText or Activity . This makes it easy to connect it to any application or activity that it needs. This custom keyboard view also uses InputConnection , which mimics the way the system keyboard interacts with EditText . This is how we avoid hard links.

MyKeyboard is a composite view that inflates the layout of the view that we defined above.

MyKeyboard.java

 public class MyKeyboard extends LinearLayout implements View.OnClickListener { // constructors public MyKeyboard(Context context) { this(context, null, 0); } public MyKeyboard(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyKeyboard(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } // keyboard keys (buttons) private Button mButton1; private Button mButton2; private Button mButton3; private Button mButton4; private Button mButton5; private Button mButton6; private Button mButton7; private Button mButton8; private Button mButton9; private Button mButton0; private Button mButtonDelete; private Button mButtonEnter; // This will map the button resource id to the String value that we want to // input when that button is clicked. SparseArray<String> keyValues = new SparseArray<>(); // Our communication link to the EditText InputConnection inputConnection; private void init(Context context, AttributeSet attrs) { // initialize buttons LayoutInflater.from(context).inflate(R.layout.keyboard, this, true); mButton1 = (Button) findViewById(R.id.button_1); mButton2 = (Button) findViewById(R.id.button_2); mButton3 = (Button) findViewById(R.id.button_3); mButton4 = (Button) findViewById(R.id.button_4); mButton5 = (Button) findViewById(R.id.button_5); mButton6 = (Button) findViewById(R.id.button_6); mButton7 = (Button) findViewById(R.id.button_7); mButton8 = (Button) findViewById(R.id.button_8); mButton9 = (Button) findViewById(R.id.button_9); mButton0 = (Button) findViewById(R.id.button_0); mButtonDelete = (Button) findViewById(R.id.button_delete); mButtonEnter = (Button) findViewById(R.id.button_enter); // set button click listeners mButton1.setOnClickListener(this); mButton2.setOnClickListener(this); mButton3.setOnClickListener(this); mButton4.setOnClickListener(this); mButton5.setOnClickListener(this); mButton6.setOnClickListener(this); mButton7.setOnClickListener(this); mButton8.setOnClickListener(this); mButton9.setOnClickListener(this); mButton0.setOnClickListener(this); mButtonDelete.setOnClickListener(this); mButtonEnter.setOnClickListener(this); // map buttons IDs to input strings keyValues.put(R.id.button_1, "1"); keyValues.put(R.id.button_2, "2"); keyValues.put(R.id.button_3, "3"); keyValues.put(R.id.button_4, "4"); keyValues.put(R.id.button_5, "5"); keyValues.put(R.id.button_6, "6"); keyValues.put(R.id.button_7, "7"); keyValues.put(R.id.button_8, "8"); keyValues.put(R.id.button_9, "9"); keyValues.put(R.id.button_0, "0"); keyValues.put(R.id.button_enter, "\n"); } @Override public void onClick(View v) { // do nothing if the InputConnection has not been set yet if (inputConnection == null) return; // Delete text or input key value // All communication goes through the InputConnection if (v.getId() == R.id.button_delete) { CharSequence selectedText = inputConnection.getSelectedText(0); if (TextUtils.isEmpty(selectedText)) { // no selection, so delete previous character inputConnection.deleteSurroundingText(1, 0); } else { // delete the selection inputConnection.commitText("", 1); } } else { String value = keyValues.get(v.getId()); inputConnection.commitText(value, 1); } } // The activity (or some parent or controller) must give us // a reference to the current EditText InputConnection public void setInputConnection(InputConnection ic) { this.inputConnection = ic; } } 

4. Point the keyboard to EditText

For system keyboards, Android uses InputMethodManager to tell the keyboard focused EditText . In this example, the action will take its place by providing a link from EditText to our custom keyboard.

Since we do not use the system keyboard, we need to disable it so that it does not pop up when you touch EditText . Secondly, we need to get an InputConnection from EditText and pass it to our keyboard.

MainActivity.java

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText editText = (EditText) findViewById(R.id.editText); MyKeyboard keyboard = (MyKeyboard) findViewById(R.id.keyboard); // prevent system keyboard from appearing when EditText is tapped editText.setRawInputType(InputType.TYPE_CLASS_TEXT); editText.setTextIsSelectable(true); // pass the InputConnection from the EditText to the keyboard InputConnection ic = editText.onCreateInputConnection(new EditorInfo()); keyboard.setInputConnection(ic); } } 

If your activity has multiple EditTexts, then you will need to write code to pass the correct EditText InputConnection to the keyboard. (You can do this by adding OnFocusChangeListener and OnClickListener to EditTexts. See this article for a discussion of this.) You can also hide or show the keyboard at an appropriate time.

Finished

It. Now you can run the sample application and enter or delete text as you wish. Your next step is to change everything to fit your needs. For example, in some of my keyboards, I used TextViews rather than Buttons, because they are easier to set up.

Notes

  • In the xml layout file, you can also use TextView and not Button if you want the keys to look better. , .
  • . , View , ViewGroup . . ( , ). , , xml . , , Key* Keyboard* . , , , .
+24
10 . '17 7:06
source share
+14
06 . '12 3:11
source share
+7
09 . '14 8:30
source share
+1
26 . '13 9:23
source share

, , . , API Android , . Suragch , . GitHub MIT. , - .

. (CustomKeyboardView), .

CustomKeyboardView xml ( ):

  <com.donbrody.customkeyboard.components.keyboard.CustomKeyboardView android:id="@+id/customKeyboardView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" /> 

EditText , :

 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val numberField: EditText = findViewById(R.id.testNumberField) val numberDecimalField: EditText = findViewById(R.id.testNumberDecimalField) val qwertyField: EditText = findViewById(R.id.testQwertyField) keyboard = findViewById(R.id.customKeyboardView) keyboard.registerEditText(CustomKeyboardView.KeyboardType.NUMBER, numberField) keyboard.registerEditText(CustomKeyboardView.KeyboardType.NUMBER_DECIMAL, numberDecimalField) keyboard.registerEditText(CustomKeyboardView.KeyboardType.QWERTY, qwertyField) } 

CustomKeyboardView !

Number, NumberDecimal QWERTY. . It looks like this:

android custom keyboard gif landscape

enter image description here

, , , .

, : Custom In-App Keyboard

+1
23 . '18 2:07
source share

, , , .

, , . , .

apk Android APK Builder 1.1.0. , .

Android, , .

Res layout β†’ XML , , . , HTML - . .

β†’ , colors.xml, strings.xml, styles.xml. .

drawable β†’ pics {jpeg, png,...}; .

mipmap β†’ . ?

xml β†’ xml .

src β†’ JavaScript html. , java . layout.xml, html.

AndroidManifest.xml β†’ . , , .. Android . , .

4 Android: , , . , . ; .

gradle apk-. APK Builder Android. .

, Android, .

  1. , . :

    • NumPad
      • AndroidManifest.xml
        • Saragch
          • NUM_PAD
            • MyInputMethodService.java
          • Suragch_NumPad_icon.png
        • location
          • key_preview.xml
          • keyboard_view.xml
        • XML
          • method.xml
          • number_pad.xml
        • values
          • colors.xml
          • strings.xml
          • styles.xml

, ide, Android Studio, .

  1. .

A: NumPad/res/layout/key_preview.xml

 <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="@android:color/white" android:textColor="@android:color/black" android:textSize="30sp"> </TextView> 

B: NumPad/res/layout/keyboard_view.xml

 <?xml version="1.0" encoding="utf-8"?> <android.inputmethodservice.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/keyboard_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:keyPreviewLayout="@layout/key_preview" android:layout_alignParentBottom="true"> </android.inputmethodservice.KeyboardView> 

C: NumPad/res/xml/method.xml

 <?xml version="1.0" encoding="utf-8"?> <input-method xmlns:android="http://schemas.android.com/apk/res/android"> <subtype android:imeSubtypeMode="keyboard"/> </input-method> 

D: Numpad/res/xml/number_pad.xml

 <?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyWidth="20%p" android:horizontalGap="5dp" android:verticalGap="5dp" android:keyHeight="60dp"> <Row> <Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/> <Key android:codes="50" android:keyLabel="2"/> <Key android:codes="51" android:keyLabel="3"/> <Key android:codes="52" android:keyLabel="4"/> <Key android:codes="53" android:keyLabel="5" android:keyEdgeFlags="right"/> </Row> <Row> <Key android:codes="54" android:keyLabel="6" android:keyEdgeFlags="left"/> <Key android:codes="55" android:keyLabel="7"/> <Key android:codes="56" android:keyLabel="8"/> <Key android:codes="57" android:keyLabel="9"/> <Key android:codes="48" android:keyLabel="0" android:keyEdgeFlags="right"/> </Row> <Row> <Key android:codes="-5" android:keyLabel="DELETE" android:keyWidth="40%p" android:keyEdgeFlags="left" android:isRepeatable="true"/> <Key android:codes="10" android:keyLabel="ENTER" android:keyWidth="60%p" android:keyEdgeFlags="right"/> </Row> </Keyboard> 

, . .

, Android Studio; . , APK Builder.

E: NumPad/res/values /colors.xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> </resources> 

F: NumPad/res/values /strings.xml

 <resources> <string name="app_name">Suragch NumPad</string> </resources> 

G: NumPad/res/values /styles.xml

 <resources> <!-- Base application theme. --> <style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar"> <!-- Customize your theme here. --> </style> </resources> 

H: Numpad/AndroidManifest.xml

, . , . . . Suracgh, , . , Android. - . numpad ! .

. , ! xmlns: android -sdk; . , .

, , . service.android:name java . . package - , java .

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="Saragch.num_pad"> <uses-sdk android:minSdkVersion="12" android:targetSdkVersion="27" /> <application android:allowBackup="true" android:icon="@drawable/Suragch_NumPad_icon" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <service android:name=".MyInputMethodService" android:label="Keyboard Display Name" android:permission="android.permission.BIND_INPUT_METHOD"> <intent-filter> <action android:name="android.view.InputMethod"/> </intent-filter> <meta-data android:name="android.view.im" android:resource="@xml/method"/> </service> </application> </manifest> 

: NumPad/src/Saragch/num_pad/MyInputMethodService.java

: , Java src.

, , . , Java , , , . xml Android!

, ! , "" , , ! InputMethodService, Keyboard .. , . , , , ?

. , , , .

 package Saragch.num_pad; import android.inputmethodservice.InputMethodService; import android.inputmethodservice.KeyboardView; import android.inputmethodservice.Keyboard; import android.text.TextUtils; import android.view.inputmethod.InputConnection; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MyInputMethodService extends InputMethodService implements KeyboardView.OnKeyboardActionListener { @Override public View onCreateInputView() { // get the KeyboardView and add our Keyboard layout to it KeyboardView keyboardView = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard_view, null); Keyboard keyboard = new Keyboard(this, R.xml.number_pad); keyboardView.setKeyboard(keyboard); keyboardView.setOnKeyboardActionListener(this); return keyboardView; } @Override public void onKey(int primaryCode, int[] keyCodes) { InputConnection ic = getCurrentInputConnection(); if (ic == null) return; switch (primaryCode) { case Keyboard.KEYCODE_DELETE: CharSequence selectedText = ic.getSelectedText(0); if (TextUtils.isEmpty(selectedText)) { // no selection, so delete previous character ic.deleteSurroundingText(1, 0); } else { // delete the selection ic.commitText("", 1); } ic.deleteSurroundingText(1, 0); break; default: char code = (char) primaryCode; ic.commitText(String.valueOf(code), 1); } } @Override public void onPress(int primaryCode) { } @Override public void onRelease(int primaryCode) { } @Override public void onText(CharSequence text) { } @Override public void swipeLeft() { } @Override public void swipeRight() { } @Override public void swipeDown() { } @Override public void swipeUp() { } } 
  1. .

    , Android. , , .

, Gradle apk. apk jar rar zip . . , .

, . . , .

0
24 '19 9:16
source share



All Articles