编程按钮以获取下一个焦点



我正在尝试编程一个按钮,以将焦点设置为NextFocusDown命名的对象。目前,我的.xml文件具有以下

<Button
    android:id="@+id/foo_button"
    android:nextFocusDown="@id/bar_button"/>

我想在屏幕上创建一个按钮,当选择FOO时,请转到bar。我尝试使用这样的关键

public avoid downButtonPressed(View view)
{
    dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN));
    dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_DOWN));
}

这不是在做技巧。

尝试此样本。属性setFocusableIntouchmode =" true"用于视觉焦点

布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/foo_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/bar_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="true"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/foo_button" />
</android.support.constraint.ConstraintLayout>

代码:

package com.example.myapplication
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        foo_button.setOnClickListener {
            bar_button.requestFocus()
        }
    }
}

使用数据绑定的更多通用方式

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
        <Button
            android:id="@+id/foo_button"
            nextFocus="@{barButton}"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:nextFocusDown="@id/bar_button"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        <Button
            android:id="@+id/bar_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusableInTouchMode="true"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/foo_button" />
    </android.support.constraint.ConstraintLayout>
</layout>

ViewBindingAdapters.kt

package com.example.myapplication
import android.databinding.BindingAdapter
import android.view.View
@BindingAdapter("nextFocus")
fun setNextFocus(view: View, targetView: View) {
    view.setOnClickListener {
        targetView.requestFocus()
    }
}

将此行添加到应用程序build.gradle

apply plugin: 'kotlin-kapt'

Kotlin Extensions的更通用方式

package com.example.myapplication
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        foo_button.setNextFocus(bar_button) {
            // Your additional click logic
        }
    }
}

viewExtensions.kt

package com.example.myapplication
import android.view.View
fun View.setNextFocus(target: View, onClick: () -> Unit) {
    setOnClickListener {
        onClick()
        target.requestFocus()
    }
}

最新更新