【问题标题】:Android/Kotlin: show a list of paired bluetooth devicesAndroid/Kotlin:显示配对的蓝牙设备列表
【发布时间】:2020-06-05 14:03:06
【问题描述】:

我需要让用户选择配对的蓝牙设备。

我需要这样做两次:

  1. 在之前甚至没有选择任何适配器的情况下的模态对话中,并且
  2. 在我的设置屏幕上。

这是我的MainActivity.kt 中的 (1)。

    fun showDeviceSelector() {
        val btManager: BluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager;
        val btAdapter: BluetoothAdapter = btManager.getAdapter();
        val pairedDevices: Set<BluetoothDevice>? = btAdapter.bondedDevices;
        // (There is one.)

        val cs: Array<CharSequence> = pairedDevices.map { z -> z.name }.toTypedArray()
        var deviceName: String = ""

        val ab = AlertDialog.Builder(this);
        ab.setTitle("Select device");
        ab.setIcon(android.R.drawable.ic_dialog_alert);
        ab.setPositiveButton("Select") { dialogueInterface, which ->
            btConnect(deviceName);
        };
        ab.setNeutralButton("Cancel") { dialogueInterface, which ->
            Toast.makeText(
                applicationContext,
                "No Bluetooth adapter selected.",
                Toast.LENGTH_LONG
            ).show()
        };
        ab.setItems(cs) { dialog, which -> deviceName = cs[which].toString() };
        val a = ab.create();
        a.setCancelable(false);
        a.show();
    }

它显示一个对话,但没有下拉菜单。为什么?

(对不起,这不是截图,我无法让模拟器工作,所以我使用的是真实设备。)

对于 (2) 任何指针将不胜感激 - 我是 Android 新手,但在 C#/SQL 等方面有 15 年的经验,我发现它异常困难。)

【问题讨论】:

    标签: android-studio android-layout kotlin


    【解决方案1】:

    问题是列表项文本也是黑色的。

    这是应该被覆盖使用

    <item name="android:textColorHint">@color/text</item>
    

    但是,通常在 Android 开发中,这不起作用。

    【讨论】:

      猜你喜欢
      • 2015-03-06
      • 2015-07-30
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 2019-05-19
      • 2011-12-15
      相关资源
      最近更新 更多