Android界面设计之创建自定义的控件焦点顺序

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

步骤1 定义界面布局

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

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

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

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

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

  下面是其代码:

< ?xml version = " 1.0 "  encoding = " utf-8 " ? >
< RelativeLayout
    xmlns:android
= " http://schemas.android.com/apk/res/android "
    android:layout_width
= " fill_parent "
    android:layout_height
= " fill_parent " >
    
< Button
        style
= " @style/clockFaceNum "
        android:text
= " 12 "
        android:id
= " @+id/button12 "
        android:layout_alignParentTop
= " true "
        android:layout_centerHorizontal
= " true "
        android:nextFocusUp
= " @+id/button11 "
        android:nextFocusLeft
= " @+id/button11 "
        android:nextFocusRight
= " @+id/button1 "
        android:nextFocusDown
= " @+id/button1 " >
    
</ Button >
    
< Button
        style
= " @style/clockFaceNum "
        android:text
= " 11 "
        android:id
= " @+id/button11 "
        android:layout_below
= " @+id/button12 "
        android:layout_toLeftOf
= " @+id/button12 "
        android:nextFocusUp
= " @+id/button10 "
        android:nextFocusLeft
= " @+id/button10 "
        android:nextFocusRight
= " @+id/button12 "
        android:nextFocusDown
= " @+id/button12 " >
    
</ Button >
    
< Button
        style
= " @style/clockFaceNum "
        android:text
= " 1 "
        android:id
= " @+id/button1 "
        android:layout_below
= " @+id/button12 "
        android:layout_toRightOf
= " @+id/button12 "
        android:nextFocusUp
= " @+id/button12 "
        android:nextFocusLeft
= " @+id/button12 "
        android:nextFocusRight
= " @+id/button2 "
        android:nextFocusDown
= " @+id/button2 " >
    
</ Button >
    
< Button
        style
= " @style/clockFaceNum "
        android:text
= " 10 "
        android:id
= " @+id/button10 "
        android:layout_below
= " @+id/button11 "
        android:layout_toLeftOf
= " @+id/button11 "
        android:nextFocusUp
= " @+id/button9 "
        android:nextFocusLeft
= " @+id/button9 "
        android:nextFocusRight
= " @+id/button11 "
        android:nextFocusDown
= " @+id/button11 " >
    
</ Button >
    
< Button
        style
= " @style/clockFaceNum "
        android:text
= " 2 "
        android:id
= " @+id/button2 "
        android:layout_below
= " @+id/button1 "
        android:layout_toRightOf
= " @+id/button1 "
        android:nextFocusUp
= " @+id/button1 "
        android:nextFocusLeft
= " @+id/button1 "
        android:nextFocusRight
= " @+id/button3 "
        android:nextFocusDown
= " @+id/button3 " >
    
</ Button >
    
< Button
        style
= " @style/clockFaceNum "
        android:text
= " 9 "
        android:id
= " @+id/button9 "
        android:layout_below
= " @+id/button10 "
        android:layout_toLeftOf
= " @+id/button10 "
        android:nextFocusUp
= " @+id/button8 "
        android:nextFocusLeft
= " @+id/button8 "
        android:nextFocusRight
= " @+id/button10 "
        android:nextFocusDown
= " @+id/button10 " >
    
</ Button >

    
< Button
        style
= " @style/clockFaceNum "
        android:text
= " 3 "
        android:id
= " @+id/button3 "
        android:layout_below
= " @+id/button2 "
        android:layout_toRightOf
= " @+id/button2 "
        android:nextFocusUp
= " @+id/button2 "
        android:nextFocusLeft
= " @+id/button2 "
        android:nextFocusRight
= " @+id/button4 "
        android:nextFocusDown
= " @+id/button4 " >
    
</ Button >
    
< Button
        style
= " @style/clockFaceNum "
        android:text
= " 8 "
        android:id
= " @+id/button8 "
        android:layout_below
= " @+id/button9 "
        android:layout_toRightOf
= " @+id/button9 "
        android:nextFocusUp
= " @+id/button7 "
        android:nextFocusLeft
= " @+id/button7 "
        android:nextFocusRight
= " @+id/button9 "
        android:nextFocusDown
= " @+id/button9 " >
    
</ Button >
    
< Button
        style
= " @style/clockFaceNum "
        android:text
= " 4 "
        android:id
= " @+id/button4 "
        android:layout_below
= " @+id/button3 "
        android:layout_toLeftOf
= " @+id/button3 "
        android:nextFocusUp
= " @+id/button3 "
        android:nextFocusLeft
= " @+id/button3 "
        android:nextFocusRight
= " @+id/button5 "
        android:nextFocusDown
= " @+id/button5 " >
    
</ Button >
    
< Button
        style
= " @style/clockFaceNum "
        android:text
= " 7 "
        android:id
= " @+id/button7 "
        android:layout_below
= " @+id/button8 "
        android:layout_toRightOf
= " @+id/button8 "
        android:nextFocusUp
= " @+id/button6 "
        android:nextFocusLeft
= " @+id/button6 "
        android:nextFocusRight
= " @+id/button8 "
        android:nextFocusDown
= " @+id/button8 " >
    
</ Button >
    
< Button
        style
= " @style/clockFaceNum "
        android:text
= " 5 "
        android:id
= " @+id/button5 "
        android:layout_below
= " @+id/button4 "
        android:layout_toLeftOf
= " @+id/button4 "
        android:nextFocusUp
= " @+id/button4 "
        android:nextFocusLeft
= " @+id/button4 "
        android:nextFocusRight
= " @+id/button6 "
        android:nextFocusDown
= " @+id/button6 " >
    
</ Button >
    
< Button
        style
= " @style/clockFaceNum "
        android:text
= " 6 "
        android:id
= " @+id/button6 "
        android:layout_below
= " @+id/button5 "
        android:layout_centerHorizontal
= " true "
        android:nextFocusUp
= " @+id/button5 "
        android:nextFocusLeft
= " @+id/button5 "
        android:nextFocusRight
= " @+id/button7 "
        android:nextFocusDown
= " @+id/button7 " >
    
</ Button >
</ RelativeLayout >

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

步骤1 定义界面布局

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
设计自定义控件需要以下步骤: 1.创建一个新的 Android Studio 项目。 2.在项目中创建自定义控件的类。该类应继承自 View 或其子类(例如 Button、EditText 等)。 3.在自定义控件类中实现构造函数和必要的方法,例如 onMeasure()、onLayout() 和 onDraw() 等。 4.在布局文件中使用自定义控件。 下面是一个示例代码: ``` public class MyCustomView extends View { private Paint paint; private Rect rect; public MyCustomView(Context context) { super(context); init(); } public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { paint = new Paint(); paint.setColor(Color.BLUE); rect = new Rect(0, 0, 100, 100); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int desiredWidth = 200; int desiredHeight = 200; int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int width; int height; if (widthMode == MeasureSpec.EXACTLY) { width = widthSize; } else if (widthMode == MeasureSpec.AT_MOST) { width = Math.min(desiredWidth, widthSize); } else { width = desiredWidth; } if (heightMode == MeasureSpec.EXACTLY) { height = heightSize; } else if (heightMode == MeasureSpec.AT_MOST) { height = Math.min(desiredHeight, heightSize); } else { height = desiredHeight; } setMeasuredDimension(width, height); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawRect(rect, paint); } } ``` 在布局文件中使用自定义控件: ``` <com.example.myapplication.MyCustomView android:layout_width="wrap_content" android:layout_height="wrap_content"/> ``` 以上代码是一个简单的自定义 View,它绘制一个蓝色的矩形。当然,你可以根据需要修改自定义控件的代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值