【问题标题】:Fix focus for controls on Android TV修复 Android TV 上控件的焦点
【发布时间】:2022-10-15 07:56:39
【问题描述】:

我花了很多时间来理解为什么它不起作用,但仍然不知道如何解决它。 我找到了这个解决方案 https://github.com/flutter/flutter/issues/49783 ,但它对我没有帮助。 我的问题是按下 RC 上的选择按钮时开关不会改变(我正在使用模拟器)。 重现步骤:

  • 启动应用程序
  • 在第一个文本控件中输入一些文本(在我的情况下,我需要按向下和向上按钮来显示键盘,这是另一个错误,可以很酷地修复)
  • 按回 RC 按钮(顺便说一句,如果您知道如何使用提交,请告诉我)隐藏键盘
  • 按下 RC 按钮 - 焦点将切换
  • 然后尝试按下选择按钮(它是第一次工作)
  • 然后返回文本字段并按返回然后向下 RC 按钮
  • 现在按 Select 时开关不起作用
  • 按下选择按钮仍然不起作用
  • 按下 - 选择正在切换开关,按钮再次起作用! 如果你去文本字段,那么它会重复

我的示例代码:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return Shortcuts(
      shortcuts: <LogicalKeySet, Intent>{
        LogicalKeySet(LogicalKeyboardKey.select): const ActivateIntent(),
      },
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: const MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool switchValue = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter Demo Home Page'),
      ),
      body: Center(
        child: Column(
          // mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: EdgeInsets.symmetric(vertical: 10),
              child: Focus(
                canRequestFocus: false,
                onKey: (FocusNode node, RawKeyEvent event) {
                  if (event.logicalKey == LogicalKeyboardKey.arrowLeft) {
                    FocusManager.instance.primaryFocus!
                        .focusInDirection(TraversalDirection.left);
                  } else if (event.logicalKey ==
                      LogicalKeyboardKey.arrowRight) {
                    FocusManager.instance.primaryFocus!
                        .focusInDirection(TraversalDirection.right);
                  } else if (event.logicalKey == LogicalKeyboardKey.arrowUp) {
                    FocusManager.instance.primaryFocus!
                        .focusInDirection(TraversalDirection.up);
                  } else if (event.logicalKey == LogicalKeyboardKey.arrowDown) {
                    FocusManager.instance.primaryFocus!
                        .focusInDirection(TraversalDirection.down);
                  }
                  return KeyEventResult.handled;
                },
                child: TextField(
                  autofocus: true,
                ),
              ),
            ),
            Switch(
              value: switchValue,
              onChanged: (value) {
                setState(() {
                  switchValue = value;
                });
              },
            ),
            TextButton(
              onPressed: () => print('Button pressed'),
              style: TextButton.styleFrom(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(18.0),
                ),
              ),
              child: const Text('Test'),
            ),
          ],
        ),
      ),
    );
  }
}

这是我的 AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testfocus.test_focus">
<application
     android:label="test_focus"
     android:name="${applicationName}"
     android:icon="@mipmap/ic_launcher">
    <activity
        android:name=".MainActivity"
        android:exported="true"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
        <!-- Specifies an Android theme to apply to this Activity as soon as
             the Android process has started. This theme is visible to the user
             while the Flutter UI initializes. After that, this theme continues
             to determine the Window background behind the Flutter UI. -->
        <meta-data
          android:name="io.flutter.embedding.android.NormalTheme"
          android:resource="@style/NormalTheme"
          />
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <!-- Don't delete the meta-data below.
         This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
    <meta-data
        android:name="flutterEmbedding"
        android:value="2" />
</application>
<uses-feature android:name="android.software.leanback" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />

【问题讨论】:

    标签: android flutter focus textfield television


    【解决方案1】:

    我在使用颤振开发电视应用程序时遇到了类似的问题。

    由于文本字段有它自己的焦点,我找到了一种解决方法。

    我禁用了文本字段的输入,并出现在屏幕键盘上以在文本字段中输入值。

    例如这个:

    根据按钮按下字符在文本字段上分配值,这将是一个很好的解决方法并且更加用户友好。 在使用 Flutter 开发电视应用程序时尽量减少用户输入。

    【讨论】:

      猜你喜欢
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 2020-10-13
      • 2018-07-20
      相关资源
      最近更新 更多