對焦點(diǎn)的處理是Android 電視開發(fā)與手機(jī)開發(fā)最大的區(qū)別之一筹裕。在手機(jī)上醋闭,關(guān)于焦點(diǎn)我們幾乎不需要做額外的處理;而在電視機(jī)上則需要做大量的處理朝卒,如:焦點(diǎn)的顯示樣式(顏色证逻、大小等)、焦點(diǎn)的導(dǎo)航抗斤、顯示動畫等囚企。
focusable與focusableInTouchMode
在xml中對view設(shè)置屬性:
android:focusable="true"
android:focusableInTouchMode="true"
動態(tài)設(shè)置:
View.setFocusable(true);
View.setFocusableInTouchMode(true)
上述屬性focusable設(shè)置為true時(shí)當(dāng)前控件才能獲得焦點(diǎn)丈咐;反之,不能獲得焦點(diǎn)龙宏。focusableInTouchMode用于設(shè)置是否可以通過觸摸獲取焦點(diǎn)棵逊。在手機(jī)應(yīng)用開發(fā)中一般不需要對上述屬性進(jìn)行設(shè)置,電視應(yīng)用很有必要银酗,因?yàn)殡娨曂ǔJ峭ㄟ^遙控器來控制焦點(diǎn)的(也可以是鼠標(biāo))辆影。
requestFocus
動態(tài)設(shè)置:
View.requestFocus();
xml中:
<requestFocus />
為控件設(shè)置焦點(diǎn),通常會優(yōu)先獲取焦點(diǎn)(可能某些控件會搶奪焦點(diǎn)黍特,如EditText)蛙讥。打開頁面時(shí),通常會使用它來決定誰優(yōu)先獲得焦點(diǎn)灭衷。
descendantFocusability
當(dāng)焦點(diǎn)獲取出現(xiàn)問題是你可以嘗試設(shè)置descendantFocusability屬性 次慢,有以下三中值:
beforeDescendants:viewgroup會優(yōu)先其子類控件而獲取到焦點(diǎn)
afterDescendants:viewgroup只有當(dāng)其子類控件不需要獲取焦點(diǎn)時(shí)才獲取焦點(diǎn)
blocksDescendants:viewgroup會覆蓋子類控件而直接獲得焦點(diǎn)
建議簡化布局層級,層級過多會導(dǎo)致焦點(diǎn)處理變得復(fù)雜翔曲,也很難實(shí)現(xiàn)特殊的焦點(diǎn)選中效果迫像。