如何在输入视图AlertDialog中添加setText

时间:2013-12-24 16:17:51

标签: android file rename alertdialog

我编写了一个重命名文件的算法,如何在输入字段中打印当前名称?

 final EditText input = new EditText(MainActivity.this);
                                new AlertDialog.Builder(MainActivity.this)
                                        .setTitle("Rename")
                                        .setMessage("Add new name:")
                                        .setView(input)
                                        .setPositiveButton("Ok", new OnClickListener() {
                                            public void onClick(DialogInterface dialog, int whichButton) {
                                                String value = input.getText().toString();
                                                if (directory.isDirectory()) {
                                                    File from = new File(directory.getAbsolutePath());
                                                    File to = new File(directory.getParent() + "/" + value);
                                                    from.renameTo(to);
                                                } else {
                                                    File from = new File(directory.getAbsolutePath());
                                                    File to = new File(directory.getParent() + "/" + value + "." + checkFormat);
                                                    from.renameTo(to);
                                                }
                                                go(currentDirectory);
                                            }
                                        }).setNegativeButton("Cancel", new OnClickListener() {
                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        // Do nothing.
                                    }
                                }).show();

我非常感谢帮助

1 个答案:

答案 0 :(得分:1)

如果我理解正确,这应该可以将旧名称放在输入框中。

File old = null;
if (directory.isDirectory()) {                                           
    old = new File(directory.getAbsolutePath());
}
final EditText input = new EditText(MainActivity.this);
input.setText(old.toString());
...
//the rest of your code