首页 > 解决方案 > NativeScript 和方向键导航

问题描述

如何使用原生 Android 方法(此处描述:https ://developer.android.com/training/tv/start/navigation在“启用方向键导航”部分,例如 nextFocusDown、nextFocusLeft 等)?我在我的代码中使用动态 id 分配。

这是一个例子:

<ListView class="list-group" for="(country,index) in countries" style="height:1250px">
<v-template>
    <FlexboxLayout flexDirection="row" class="list-group-item">
        <Image  :src="country.imageSrc" class="thumb img-circle" />
        <Button :id="'button'+index" @loaded="elementLoaded(index,$event)" text="Tap Me!" class="btn btn-primary btn-active" />
        <Label  :id="'label'+index" :text="index + country.name" style="color: red; width: 60%" />
    </FlexboxLayout>
</v-template>

export default {
    data() {
        return {
            countries: [
                {
                    name: "Australia",
                    imageSrc:
                    "https://play.nativescript.org/dist/assets/img/flags/au.png"
                },
                {
                    name: "Belgium",
                    imageSrc:
                    "https://play.nativescript.org/dist/assets/img/flags/be.png"
                }
            ]
        }
    },
    methods: {
        elementLoaded(index,args) {
            args.object.nextFocusDown("button"+(index+1))
            // How can I set native Android id to this element (like a Button) with help of NativeScript?
            // Also I need to set native Android attribute like "NextFocusDown" to this element (like a Button).
        },
    }
}

标签: androidvue.jsnativescript

解决方案


You can always access the native element by accessing the native view property.

// nativeView will be the native iOS / Android button and you are free to access any of it's native methods 
args.object.nativeView 

推荐阅读