控件焦點相關(guān)問題
一、啟用焦點停留 (tab stop)
除了 edit 控件外,其它控件如果需要焦點停留功能,可以指定控件的 focusable 屬性為 true 來實現(xiàn)。
在 XML 中,可以這樣指定:
<button ... focusable="true"/>
在 C 代碼中,可以這樣指定:
widget_set_prop_bool(widget, WIDGET_PROP_FOCUSABLE, TRUE);
如果指定了 fucusable 屬性為 true,請確保控件的 style 中定義了 focused 狀態(tài)的數(shù)據(jù),否則會因為 focused 狀態(tài)沒有 style 數(shù)據(jù)而無法顯示。如:
<style name="default" border_color="#a0a0a0" text_color="black">
<normal bg_color="#f0f0f0" />
<pressed bg_color="#c0c0c0" x_offset="1" y_offset="1"/>
<over bg_color="#e0e0e0" />
<focused bg_color="#e0e0e0" />
<disable bg_color="gray" text_color="#d0d0d0" />
</style> <focused bg_color="#e0e0e0" />
二、前后切換焦點的按鍵
1. 缺省用 tab 鍵循環(huán)切換焦點。
#ifndef TK_KEY_MOVE_FOCUS_NEXT
#define TK_KEY_MOVE_FOCUS_NEXT "tab"
#endif /*TK_KEY_MOVE_FOCUS_NEXT*/
2. 可以為當前窗口指定的向前和向后移動焦點的鍵值。
- move_focus_prev_key 指定向前移動焦點的鍵值。
- move_focus_next_key 指定向后移動焦點的鍵值。
<window anim_hint="htranslate" move_focus_prev_key="up" move_focus_next_key="down">
在這個例子中,方向鍵 up 移動到前一個焦點控件,方向鍵 down 移動到下一個焦點控件。
三、上下左右切換焦點的按鍵
在一些特殊的硬件設備上,沒有觸摸屏,只有上、下、左、右、確定和取消六個鍵。
為了快速切換焦點,AWTK 支持通過左右鍵切換水平焦點,通過上下鍵切換垂直焦點??梢酝ㄟ^窗口的下列屬性來設置:
- move_focus_up_key 向上移動焦點的鍵。
- move_focus_down_key 向下移動焦點的鍵。
- move_focus_left_key 向左移動焦點的鍵。
- move_focus_right_key 向右移動焦點的鍵。
示例:
<window text="Custom Soft Keyboard" anim_hint="htranslate"
move_focus_up_key="up" move_focus_down_key="down" move_focus_left_key="left" move_focus_right_key="right">
<edit name="edit" x="c" y="10" w="90%" h="30" focused="true" input_type="custom" text="" tips="custom"/>
<view y="60" x="c" w="90%" h="-60" is_keyboard="true" grab_keys="true"
children_layout="default(r=4,c=4,m=5,s=5)" >
<button focusable="true" name="key0" text="0" />
<button focusable="true" name="key1" text="1" />
<button focusable="true" name="key2" text="2" />
<button focusable="true" name="key3" text="3" />
<button focusable="true" name="key4" text="4" />
<button focusable="true" name="key5" text="5" />
<button focusable="true" name="key6" text="6" />
<button focusable="true" name="key7" text="7" />
<button focusable="true" name="key8" text="8" />
<button focusable="true" name="key9" text="9" />
<button focusable="true" name="key#" text="#" />
<button focusable="true" name="backspace" text="<=" />
</view>
</window>
在這個例子中,方向鍵 up 移動到上方的焦點控件,方向鍵 down 移動到下方的焦點控件。
方向鍵 left 移動到左方的焦點控件,方向鍵 right 移動到右方的焦點控件。
軟鍵盤本身不能得到焦點,為了收到按鍵消息,需要指定屬性 grab_keys="true"。
四、設置初始焦點
可以指定控件的 focused 屬性為 true 將控件設置為初始焦點控件。
在 XML 中,可以這樣指定:
<button ... focused="true"/>
在 C 中,可以這樣指定:
widget_set_prop_bool(widget, WIDGET_PROP_FOCUSED, TRUE);