我有一个菜单选项,允许用户设置蓝牙设备名称。目前,对话框实际上并未在选中时显示设备的当前名称。我想在可编辑文本字段中向用户显示现有设备名称,以便用户在更改设备之前可以查看设备名称。
目前的方法如下所示。
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();
}
答案 0 :(得分: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();
}