如何在Android中制作自定义键盘?

如何在Android中制作自定义键盘?,android,keyboard,Android,Keyboard,我想做一个定制键盘。我不知道如何使用XML和Java实现它。下图是我想制作的键盘模型。它只需要数字 这里是一个软键盘的示例项目 你的应该是在同一行不同的布局 编辑: 如果你只需要在你的应用程序中使用键盘,它非常简单! 创建垂直方向的线性布局,并在其内部创建3个水平方向的线性布局。 然后将每行的按钮放置在这些水平线性布局中,并为按钮指定权重属性。使用android:layou\u weight=1来表示所有的布局,这样它们的间距就相等了 这将解决问题。如果您没有得到预期的结果,请在这里发布代码,

我想做一个定制键盘。我不知道如何使用XML和Java实现它。下图是我想制作的键盘模型。它只需要数字


这里是一个软键盘的示例项目

你的应该是在同一行不同的布局

编辑: 如果你只需要在你的应用程序中使用键盘,它非常简单! 创建垂直方向的线性布局,并在其内部创建3个水平方向的线性布局。 然后将每行的按钮放置在这些水平线性布局中,并为按钮指定权重属性。使用android:layou\u weight=1来表示所有的布局,这样它们的间距就相等了

这将解决问题。如果您没有得到预期的结果,请在这里发布代码,我们将在这里帮助您

使用:

现在您有了
kbd
,这是一个普通视图


这方面的好处是
R.xml.custom
指的是
/res/xml/custom.xml
,它在xml中定义了键盘的布局。有关此文件的详细信息,请查看此处:,。

也有相同的问题。起初我使用的是表格布局,但按下按钮后布局一直在变化。不过我觉得这个页面很有用

这是我发现的最好的有充分证据的例子之一


KeyboardView
提供了相关的XML文件和源代码。

首先,您需要一个
keyboard.XML
文件,该文件将放置在
res/XML
文件夹中(如果该文件夹不存在,则创建它)

**请注意,为了能够设置
alignParentBottom=“true”
(通常键盘显示在屏幕底部),您将放置
android.inputmethodservice.KeyboardView
的xml文件必须是
RelativeLayout

然后,您需要在
活动
onCreate
功能中添加以下代码,该活动处理要将键盘连接到的
文本视图

    // 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);
**请注意,
mKeyboard
mKeyboardView
是必须创建的私有类变量

然后需要以下函数来打开键盘(必须通过
onClick
xml属性将其与TextView关联)

最后,您需要处理事件的
OnKeyboardActionListener

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() {
    }
};
希望有帮助

大部分代码都被找到了

____________________________________________________________-

编辑:

由于KeyboardView从API级别29开始贬值,因此在实现上述键盘之前,您可以在中找到其代码并在代码中创建一个类。

System keyboard 这个答案告诉我们如何制作一个定制的系统键盘,可以在用户安装在手机上的任何应用程序中使用。如果你想制作一个只在你自己的应用程序中使用的键盘,那么

下面的示例如下所示。您可以为任何键盘布局修改它

以下步骤显示如何创建可工作的自定义系统键盘。我尽可能地删除任何不必要的代码。如果您还需要其他功能,最后我提供了更多帮助的链接

1.启动一个新的Android项目 我将我的项目命名为“自定义键盘”。随便你叫什么都行。这里没有什么特别的。我将保持
MainActivity
和“HelloWorld!”布局不变

2.添加布局文件 将以下两个文件添加到应用程序的
res/layout
文件夹:

  • keyboard_view.xml
  • key_preview.xml
keyboard\u view.xml

这个视图就像一个容器,可以容纳我们的键盘。在本例中,只有一个键盘,但您可以添加其他键盘,并将其插入和移出

以下是一些需要注意的事项:

  • :这是每个键的默认宽度。
    20%p
    表示每个键应占据p区域宽度的20%。但是,它可以被单个键覆盖,正如您在第三行中看到的Delete和Enter键
  • :此处是硬编码的,但您可以使用类似于
    @dimen/key\u height
    的功能来为不同的屏幕大小动态设置它
  • :水平和垂直间隙表示按键之间要留出多少空间。即使将其设置为
    0px
    ,仍有一个小间隙
  • :这可以是Unicode或自定义代码值,用于确定按键时发生的情况或输入的内容。查看是否要输入更长的Unicode字符串
  • :这是按键上显示的文本
  • :这指示键应与哪个边对齐
  • :如果按住该键,它将继续重复输入
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>
注:

  • OnKeyboardActionListener的
    监听键盘输入。在本例中,它还需要所有这些空方法
  • InputConnection
    用于将输入发送到另一个视图,如
    EditText
5.更新清单 我把它放在最后而不是第一位,因为它指的是我们上面已经添加的文件。要将自定义键盘注册为系统键盘,需要在AndroidManifest.xml文件中添加一个节。将其放入
活动
之后的
应用程序
部分

<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>



以下是总结:

  • 进入Android设置>语言和输入>当前键盘>选择键盘
  • 您应该可以在列表中看到自定义键盘。启用它
  • 返回并再次选择当前键盘。你应该看看你的客户
        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);
        }
    
    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() {
        }
    };
    
    <?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>
    
    <?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>
    
    <?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>
    
    <?xml version="1.0" encoding="utf-8"?>
    <input-method
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <subtype
            android:imeSubtypeMode="keyboard"/>
    
    </input-method>
    
    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() { }
    }
    
    <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>
    
    <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>
    
    <?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>
    
    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's InputConnection
        public void setInputConnection(InputConnection ic) {
            this.inputConnection = ic;
        }
    }
    
    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);
        }
    }
    
        <com.donbrody.customkeyboard.components.keyboard.CustomKeyboardView
        android:id="@+id/customKeyboardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />
    
    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)
    }
    
    <?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>
    
    <?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>
    
    <?xml version="1.0" encoding="utf-8"?>
    <input-method  xmlns:android="http://schemas.android.com/apk/res/android">
        <subtype  android:imeSubtypeMode="keyboard"/>
    </input-method>
    
    <?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>
    
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="colorPrimary">#3F51B5</color>
        <color name="colorPrimaryDark">#303F9F</color>
        <color name="colorAccent">#FF4081</color>
    </resources>
    
    <resources>
        <string name="app_name">Suragch NumPad</string>
    </resources>
    
    <resources>
    
        <!-- Base application theme. -->
        <style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
            <!-- Customize your theme here. -->
        </style>
    
    </resources>
    
    <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>
    
    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() { }
    }