Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在警报对话框中显示蓝牙设备名称_Android_Bluetooth_Android Alertdialog - Fatal编程技术网

Android 在警报对话框中显示蓝牙设备名称

Android 在警报对话框中显示蓝牙设备名称,android,bluetooth,android-alertdialog,Android,Bluetooth,Android Alertdialog,我有一个菜单选项,允许用户设置蓝牙设备名称。当前,当选择时,对话框不会实际显示设备的当前名称。我想在可编辑文本字段中向用户显示现有设备名称,以便用户在更改设备之前可以看到设备名称 当前方法如下所示 private void changeName() { if (D) Log.d(TAG, "changeName"); // Open the alert dialog box AlertDialog.Builder alert

我有一个菜单选项,允许用户设置蓝牙设备名称。当前,当选择时,对话框不会实际显示设备的当前名称。我想在可编辑文本字段中向用户显示现有设备名称,以便用户在更改设备之前可以看到设备名称

当前方法如下所示

private void changeName() {
        if (D)
        Log.d(TAG, "changeName");
            // Open the alert dialog box
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Change Device Name");

        // Set an EditText view to get user input
        final EditText input = new EditText(this);
        alert.setView(input);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int mButton) {
                String mDevicename = input.getText().toString();
                mBluetoothAdapter.setName(mDevicename);
                Toast.makeText(getApplicationContext(),
                        "Update device name to: " + mDevicename,
                        Toast.LENGTH_SHORT).show();

            }
        });
        alert.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int mButton) {
                        // Canceled.
                    }
                });

        alert.show();

    }

我必须用调用BluetoothAdapter类的getName()方法返回的值初始化一个字符串变量。然后我将这个字符串变量传递给AlertDialogue类的setText()方法

private void changeName() {
    if (D)
        Log.d(TAG, "changeName");
            // Open alert dialogue box
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Change Device Name");
    // Set an EditText view and insert the existing device name
    String mDevicename = mBluetoothAdapter.getName();
    final EditText input = new EditText(this);
    input.setText(mDevicename);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int mButton) {
            String mDevicename = input.getText().toString();
            mBluetoothAdapter.setName(mDevicename);
            Toast.makeText(getApplicationContext(),
                    "New device name is " + mDevicename, Toast.LENGTH_SHORT)
                    .show();

        }
    });

    alert.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int mButton) {
                    // Canceled.
                }
            });
    alert.show();
}