-1

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

当前方法如下所示。

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();

    }
4

1 回答 1

0

我必须用调用 BluetoothAdapter 类的 getName() 方法返回的值初始化一个 String 变量。然后我将此字符串变量传递给 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();
}
于 2012-12-06T19:05:24.690 回答