关于 Android 键盘 UI 的生成

2021。7。18

前情:

Android 原生输入法示例代码分析 https://blog.csdn.net/fonx/article/details/80716095


//1。在 SoftKeyboard.java step5. onCreateInputView() 中,根据 R.layout.input.xml 生成键盘 UI:

(private LatinKeyboardView mInputView;//键盘)

mInputView = (LatinKeyboardView) getLayoutInflater().inflate(R.layout.input,null);

//2。在 step7. onStartInputVIew() 中,根据 R.xml.xxx.xml 生成键盘 UI:

(private LatinKeyboard mCurKeyboard;)

mQwertyKeyboard = new LatinKeyboard(this, R.xml.qwerty_large);

//3。在 step7. onStartInputVIew() 中,

change2LartinKeyboard(); =>

mCurKeyboard = mQwertyKeyboard;

mInputView.setKeyboard(mCurKeyboard);

mInputView.setKeyLables(titles);

//注意,在 R.xml.xxx.xml 里,只定义了按键的行、列数,android:codes,而 android:keyLabel 全部定义为 " ",需要在  mInputView.setKeyLables(titles); 中指定 keyLabel。

//4。类的关系:

public class KeyboardView extends View implements View.OnClickListener // KeyboardView 是 android 一个基类

public class LatinKeyboardView extends KeyboardView //implements View.onTouchEvent

public class LatinKeyboard extends Keyboard {  // Keyboard 是 android 一个基类

        static class LatinKey extends Keyboard.Key  }

[View] => [KeyboardView] => LatinKeyboardView

[Keyboard] => LatinKeyboard

[Key] => LatinKey

[基类]

//5。这些类的命名十分令人混淆,实际上是: 

KeyboardView => KeyboardView_latin
Keyboard => Keyboard_latin
Key => Key_latin

KeyboardView_latin.setKeyboard(Keyboard_latin);
KeyboardView_latin.setKeyLables(titles);

// 在:KeyboardView_latin.java 里,有 onDraw():
this.getLatinKeyboard().getLatinKeys().get(i).onDraw(canvas,res,screenw,screenh,bshowradical);

// KeyboardView_latin 维护一个 kbdLabel,可以批量设置所有键的 kLabel,注意是一步直接设置到 Key_latin,因为 Key_latin 有成员 kLabel.
// KeyboardView_latin 也可以对所有键初始化,它还负责 onLongPress 的操作。
// 从 KeyloardView_latin 访问每个键 Key_latin:
this.getLatinKeyboard().getLatinKeys().get(i);

// 在 SoftKeyboard 里,拿到了 keylable,然后把它给到 KeyboardView_latin 的 kbdLabel,由 KeyboardView_latin 直接访问到每个键 Key_latin,对其设置 kLabel,并让其 onDraw。

// 而 Keyboard_latin 的任务是:(1)从 xml 生成键盘;(2)访问 Key_latin 的接口。(3)它有一个override : createKeyFromXml,可能是自动无须调用,就生成了所有的 Key_latin.

//6。整个的过程可以这样概括:

(1)在 主程序中,创建了一个键盘 UI: KeyboardView_latin,它应用了 xml: R.layout.input
(2)在 主程序中,创建了一个抽象的键盘: Keyboard_latin,它应用了 xml: R.xml.XXX,它还负责生成了所有的键 Key_latin,负责提供对键的访问接口。
(3)在 主程序中,通过 KeyboardView_latin.setKeyboard(Keyboard_latin),使两者绑定,这样从 KeyboardView_latin 中就能访问 Keyboard_latin,进一步能访问到各键 Key_latin。
(4)在 主程序中,读入了键名 titles,并赋值给 KeyboardView_latin 的 kbdLabel,由 keyboardView_latin 直接赋给每个键的 kLabel,以及调用各键的 onDraw


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值