Android界面设计基础:控件焦点4个步骤

Android界面设计基础:控件焦点4个步骤

现在,随着越来越多的Android的应用出现在Android Market上,如何能更加吸引用户成为摆在开发者面前的重要课题。作为Android应用,不仅要在内容上取胜,在比如界面等细节上也要很重视用户的使用体验,如果用户觉得操作困难和不符合操作习惯的话,就会认为应用不好用而不去下载或购买。在用户体验中,一些细节的问题更容易引起程序员的忽视。本文将介绍,在Android的界面设计中的各个控件的焦点顺序其中要注意的问题,这个很似简单的问题,值得开发者的重视。

AD:

Android设备有多种多样,操纵界面也有所不同,比如有触摸屏、轨迹球,传统的手机键盘等,因此开发者需要更好地了解,当用户在应用程序界面中的不同控件间移动时,各个控件的获得焦点和失去焦点的顺序,以及如何根据用户的操作习惯去自定义这些顺序。

一般情况下,Android对于特定的布局界面,会自动得出一个合适的控件焦点顺序,很多情况下是足够用的了。但是在有的情况下是有例外的。控件的下一个焦点会到达哪一个控件,主要是判断当前控件在指定的方向布局上(up/down/left/right),哪一个是最领近的控件,其扫描顺序为从左到右,从上到下,就象平时阅读书籍一样。

然而,这种顺序有时会带来一点小问题,比如当控件都布置在屏幕的上方时,如果用户再按“up”键,则不会有任何效果,同样,当控件都在屏幕下方、左边、右边时,此时再按如“down”、“Left”,“Right”键时都不会再获得控件的焦点。

在本文的例子中,将讲解如何修改默认的控件焦点顺序,以定制特定的控件切换顺序,例子中,多个按钮以一个圆形进行了排列,例子可以在

http://android-mt-tutorials.googlecode.com/svn/trunk/SimpleFocus中下载。

步骤1定义界面布局

我们先设计出界面的布局,代码如下,使用的是Relative相对布局:

version="1.0"encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

style="@style/clockFaceNum"

android:text="12"

android:id="@+id/button12"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true">

style="@style/clockFaceNum"

android:text="11"

android:id="@+id/button11"

android:layout_below="@+id/button12"

android:layout_toLeftOf="@+id/button12">

style="@style/clockFaceNum"

android:text="1"

android:id="@+id/button1"

android:layout_below="@+id/button12"

android:layout_toRightOf="@+id/button12">

style="@style/clockFaceNum"

android:text="10"

android:id="@+id/button10"

android:layout_below="@+id/button11"

android:layout_toLeftOf="@+id/button11">

style="@style/clockFaceNum"

android:text="2"

android:id="@+id/button2"

android:layout_below="@+id/button1"android:layout_toRightOf="@+id/button1">

style="@style/clockFaceNum"

android:text="9"

android:id="@+id/button9"

android:layout_below="@+id/button10"

android:layout_toLeftOf="@+id/button10">

style="@style/clockFaceNum"

android:text="3"

android:id="@+id/button3"

android:layout_below="@+id/button2"

android:layout_toRightOf="@+id/button2">

style="@style/clockFaceNum"

android:text="8"

android:id="@+id/button8"

android:layout_below="@+id/button9"

android:layout_toRightOf="@+id/button9">

style="@style/clockFaceNum"

android:text="4"

android:id="@+id/button4"

android:layout_below="@+id/button3"

android:layout_toLeftOf="@+id/button3">

style="@style/clockFaceNum"

android:text="7"

android:id="@+id/button7"

android:layout_below="@+id/button8"

android:layout_toRightOf="@+id/button8">

style="@style/clockFaceNum"

android:text="5"

android:id="@+id/button5"

android:layout_below="@+id/button4"

android:layout_toLeftOf="@+id/button4">

style="@style/clockFaceNum"

android:text="6"

android:id="@+id/button6"

android:layout_below="@+id/button5"

android:layout_centerHorizontal="true">

上面定义的style文件如下:

1.version="1.0"encoding="utf-8"?>

2.

3.

4.name="clockFaceNum">

5.

6.name="android:layout_width">38dp

7.

8.name="android:layout_height">38dp

9.

10.name="android:onClick">numClicked

11.

12.name="android:textSize">9sp

13.

14.

运行后,效果如下图:


步骤2默认的控件焦点切换顺序

比如当用户将控件焦点点在12号按钮时,点往下的“down”按钮,默认的控件焦点切换顺序如下图:


也就是说,当在按钮12上往下按的时候,控件的焦点会切换到11,接着就是键10,如此类推。

步骤3创建自定义的控件焦点顺序

下面,我们尝试创建自定义的控件焦点顺序,即同时允许在上面的界面中,当用户按键时,以顺时针或逆时针进行控件切换,如下图:


也就是说,允许用户当按“Down”或“Right”键时,切换顺序是顺时针方向,比如假设当前在键12上,按“Down”或“Right”键时,会切换到键1,而按“Up”或”Left”时,会切换到键11,如此类推。要实现这点,可以在每个按钮中进行设置如下四个属性:

android:nextFocusUp-定义当点up键时,哪个控件将获得焦点

android:nextFocusDown-定义当点down键时,哪个控件将获得焦点

android:nextFocusLeft-定义当点left键时,哪个控件将获得焦点

android:nextFocusRight--定义当点right键时,哪个控件将获得焦点

下面是其代码:

1.version="1.0"encoding="utf-8"?>

2.

3.xmlns:android="http://schemas.android.com/apk/res/android"

4.android:layout_width="fill_parent"

5.android:layout_height="fill_parent">

6.

7.style="@style/clockFaceNum"

8.android:text="12"

9.android:id="@+id/button12"

10.android:layout_alignParentTop="true"

11.android:layout_centerHorizontal="true"

12.android:nextFocusUp="@+id/button11"

13.android:nextFocusLeft="@+id/button11"

14.android:nextFocusRight="@+id/button1"

15.android:nextFocusDown="@+id/button1">

16.

17.

18.style="@style/clockFaceNum"

19.android:text="11"

20.android:id="@+id/button11"

21.android:layout_below="@+id/button12"

22.android:layout_toLeftOf="@+id/button12"

23.android:nextFocusUp="@+id/button10"

24.android:nextFocusLeft="@+id/button10"

25.android:nextFocusRight="@+id/button12"

26.android:nextFocusDown="@+id/button12">

27.

28.

29.style="@style/clockFaceNum"

30.android:text="1"

31.android:id="@+id/button1"

32.android:layout_below="@+id/button12"

33.android:layout_toRightOf="@+id/button12"

34.android:nextFocusUp="@+id/button12"

35.android:nextFocusLeft="@+id/button12"

36.android:nextFocusRight="@+id/button2"

37.android:nextFocusDown="@+id/button2">

38.

39.

40.style="@style/clockFaceNum"

41.android:text="10"

42.android:id="@+id/button10"

43.android:layout_below="@+id/button11"

44.android:layout_toLeftOf="@+id/button11"

45.android:nextFocusUp="@+id/button9"

46.android:nextFocusLeft="@+id/button9"

47.android:nextFocusRight="@+id/button11"

48.android:nextFocusDown="@+id/button11">

49.

50.

51.style="@style/clockFaceNum"

52.android:text="2"

53.android:id="@+id/button2"

54.android:layout_below="@+id/button1"

55.android:layout_toRightOf="@+id/button1"

56.android:nextFocusUp="@+id/button1"

57.android:nextFocusLeft="@+id/button1"

58.android:nextFocusRight="@+id/button3"

59.android:nextFocusDown="@+id/button3">

60.

61.

62.style="@style/clockFaceNum"

63.android:text="9"

64.android:id="@+id/button9"

65.android:layout_below="@+id/button10"

66.android:layout_toLeftOf="@+id/button10"

67.android:nextFocusUp="@+id/button8"

68.android:nextFocusLeft="@+id/button8"

69.android:nextFocusRight="@+id/button10"

70.android:nextFocusDown="@+id/button10">

71.

72.

73.

74.style="@style/clockFaceNum"

75.android:text="3"

76.android:id="@+id/button3"

77.android:layout_below="@+id/button2"

78.android:layout_toRightOf="@+id/button2"

79.android:nextFocusUp="@+id/button2"

80.android:nextFocusLeft="@+id/button2"

81.android:nextFocusRight="@+id/button4"

82.android:nextFocusDown="@+id/button4">

83.

84.

85.style="@style/clockFaceNum"

86.android:text="8"

87.android:id="@+id/button8"

88.android:layout_below="@+id/button9"

89.android:layout_toRightOf="@+id/button9"

90.android:nextFocusUp="@+id/button7"

91.android:nextFocusLeft="@+id/button7"

92.android:nextFocusRight="@+id/button9"

93.android:nextFocusDown="@+id/button9">

94.

95.

96.style="@style/clockFaceNum"

97.android:text="4"

98.android:id="@+id/button4"

99.android:layout_below="@+id/button3"

100.android:layout_toLeftOf="@+id/button3"

101.android:nextFocusUp="@+id/button3"

102.android:nextFocusLeft="@+id/button3"

103.android:nextFocusRight="@+id/button5"

104.android:nextFocusDown="@+id/button5">

105.

106.

107.style="@style/clockFaceNum"

108.android:text="7"

109.android:id="@+id/button7"

110.android:layout_below="@+id/button8"

111.android:layout_toRightOf="@+id/button8"

112.android:nextFocusUp="@+id/button6"

113.android:nextFocusLeft="@+id/button6"

114.android:nextFocusRight="@+id/button8"

115.android:nextFocusDown="@+id/button8">

116.

117.

118.style="@style/clockFaceNum"

119.android:text="5"

120.android:id="@+id/button5"

121.android:layout_below="@+id/button4"

122.android:layout_toLeftOf="@+id/button4"

123.android:nextFocusUp="@+id/button4"

124.android:nextFocusLeft="@+id/button4"

125.android:nextFocusRight="@+id/button6"

126.android:nextFocusDown="@+id/button6">

127.

128.

129.style="@style/clockFaceNum"

130.android:text="6"

131.android:id="@+id/button6"

132.android:layout_below="@+id/button5"

133.android:layout_centerHorizontal="true"

134.android:nextFocusUp="@+id/button5"

135.android:nextFocusLeft="@+id/button5"

136.android:nextFocusRight="@+id/button7"

137.android:nextFocusDown="@+id/button7">

138.

139.

下图中是假定在键12开始按down键时的焦点切换顺序:


步骤4设置界面的初始控件焦点

在每个页面加载时,可以设置界面中初始的控件焦点,以方便用户的定位操作,只需要在控件中加入即可。比如:

1.

2.style="@style/clockFaceNum"

3.android:text="12"

4.android:id="@+id/button12"

5.android:layout_alignParentTop="true"

6.android:layout_centerHorizontal="true"

7.android:nextFocusUp="@+id/button11"

8.android:nextFocusLeft="@+id/button11"

9.android:nextFocusRight="@+id/button1"

10.android:nextFocusDown="@+id/button1">

11./>

12.

小结

作为开发者,一定要记住由于Android设备的多样性,用户如何在界面上方便地进行输入或在不同的控件中来回切换是十分重要的,本文简单介绍了用户如何自定义控件的焦点切换顺序,这对于用户界面的体验是很有好处的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,547评论 4 374
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,787评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,175评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,752评论 0 223
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,169评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,056评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,195评论 2 321
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,980评论 0 214
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,724评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,872评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,344评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,667评论 3 264
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,379评论 3 245
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,202评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,992评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,189评论 2 286
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,987评论 2 279

推荐阅读更多精彩内容