首页 > 解决方案 > 如何将资产中的字体用于自定义键盘

问题描述

如何在自定义键盘中使用资产中的字体?我的意思是在键盘的按键上使用字体。

它是我的键盘主要布局:

android.inputmethodservice.KeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:keyTextColor="#ffff0000"
    android:background="@drawable/themes"
    android:keyBackground="@drawable/theme"
    android:layout_alignParentBottom="true"
    android:keyPreviewLayout="@layout/preview"
/>

我的字体名称是:

MyFont.tff

它是我的键盘活动:

package mr.saeed;
import android.inputmethodservice.*;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.view.*;
import android.view.inputmethod.*;

public class saeedyaghi extends InputMethodService
implements OnKeyboardActionListener{

    private KeyboardView kv;
    private Keyboard keyboard;

    private boolean caps = false;

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {        
        InputConnection ic = getCurrentInputConnection();
        switch(primaryCode){
            case Keyboard.KEYCODE_DELETE :
                ic.deleteSurroundingText(1, 0);
                break;
            case Keyboard.KEYCODE_SHIFT:
                caps = !caps;
                keyboard.setShifted(caps);
                kv.invalidateAllKeys();
                break;
            case Keyboard.KEYCODE_DONE:
                ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
                break;
            default:
                char code = (char)primaryCode;
                if(Character.isLetter(code) && caps){
                    code = Character.toUpperCase(code);
                }
                ic.commitText(String.valueOf(code),1);                  
        }
    }

    @Override
    public void onPress(int primaryCode) {
    }

    @Override
    public View onCreateInputView() {
        kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
        keyboard = new Keyboard(this, R.xml.fa);
        kv.setKeyboard(keyboard);
        kv.setOnKeyboardActionListener(this);
        return kv;
    }

    @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() {
    }
}

提示:我询问并搜索了很多,我明白我应该在这里做键盘上的字体:

@Override
    public View onCreateInputView() {
        kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
        keyboard = new Keyboard(this, R.xml.fa);
        kv.setKeyboard(keyboard);
        kv.setOnKeyboardActionListener(this);
        return kv;

字体系列不起作用,请不要提供它,伙计们,我想要一种实用且简单的方法,非常感谢。

标签: androidandroid-studiofontsassetsandroid-custom-keyboard

解决方案


推荐阅读