【问题标题】:include line only if platform is Android仅当平台是 Android 时才包含行
【发布时间】:2020-05-17 19:23:01
【问题描述】:

我遇到的一个问题是,当我在 android 上导航到包含表单的页面时,键盘会自动弹出。我找到了一个解决方案,但它只适用于 Android:

import { View } from "tns-core-modules/ui/core/view";

export class AutoFocusView extends View {

    createNativeView() {
        if (typeof android !== "undefined") {
            const linearLayout = new android.widget.LinearLayout(this._context);
            linearLayout.setFocusableInTouchMode(true);
            linearLayout.setFocusable(true);
            return linearLayout;
        } 
        return super.createNativeView();
    }

    onLoaded() {
        super.onLoaded();
        this.requestFocus();
    }

    requestFocus() {
        const nativeViewProtected = this.nativeViewProtected;
        nativeViewProtected.requestFocus();
    }
}

我使用这个组件。但它只适用于android,所以每次我想为IOS构建时,我都需要从我的代码中评论它。我想知道是否有更简单的方法。

【问题讨论】:

    标签: android ios nativescript nativescript-vue


    【解决方案1】:

    你可以这样做:

    import {isAndroid, isIOS} from '@nativescript/core';
    

    然后在你的计算中:

    isandroid() {
                    return isAndroid;
                }
    

    然后在您需要的任何方法中使用此标志:

    if(isAndroid) {
      this.requestFocus();
    }
    

    如果需要使用 v-if,您也可以在模板中使用这个 isAndroid 标志。

    【讨论】:

      【解决方案2】:

      仅当平台是 Android 时才调用requestFocus()

      onLoaded() {
          super.onLoaded();
          if (typeof android !== "undefined") {
              this.requestFocus();
          }
      }
      

      您也可以将其编写为项目中的插件,在特定于平台的文件中单独编写 iOS 和 Android 代码。

      【讨论】:

      • 我个人使用了您的解决方案,但我接受了另一个答案,因为我认为它更具可读性,而且我认为您不需要声誉;)谢谢:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-17
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-11
      相关资源
      最近更新 更多