Android Input Method Sample Code Analysis

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/vernice/article/details/75269179


Keyboard.xml

define the Key Label, Key code and the Row of keys.

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalGap="0dp"
    android:keyHeight="61dp"
    android:keyWidth="33%p"
    android:verticalGap="0dp">
    <Row>
        <Key
            android:codes="49"
            android:keyEdgeFlags="left"
            android:keyLabel="1" />
        <Key
            android:codes="50"
            android:keyLabel="2" />
        <Key
            android:codes="51"
            android:keyLabel="3" />
    </Row>
</Keyboard>

KeyboardControl.java

Control the behaviors of the keyboard by connecting the Keyboard.xml and an Activity instance. This KeyboardControl class also defines the function of the keyboard, like show and hide the soft keyboard, attach the input key values to the text fields.

public class KeyboardControl {
    private Activity activity;
    private KeyboardView keyboardView;
    private Keyboard keyboard;
    private EditText edText;
    public KeyboardControl(Activity activity) {
        this.mActivity = mActivity;
        this.keyboard = new Keyboard(activity, R.xml.keyboard);
    }
}

MainActivity.java

New an instance of the KeyboardControl class and the control the behaviors of the keyboard.

public class MainActivity extends AppCompatActivity {
    EditText edText;
    KeyboardControl kbControl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edText = (EditText) findViewById(R.id.edText );
        kbControl = new KeyboardControl(self());
        kbControl.attachTo(edText, false);
    }
}




猜你喜欢

转载自blog.csdn.net/vernice/article/details/75269179