我有一个应用程序,允许用户键入文本并将其导出到txt文件。当用户按下保存按钮时,会出现一个对话框,询问用户他们希望如何命名文件。用户键入一个名称,该名称被视为字符串值,然后将其与".txt"连接为最终文件名。问题是程序会立即保存文件,而无需等待用户输入名称,因此它将使用字符串的先前值,即使它是 null。当它为空时,文件名将为"null.txt"。下次用户尝试保存文件时,应用程序将立即保存文件并使用之前在第一次尝试中输入的值,并像这样继续下去。
保存按钮:
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog(); //Calls for the dialog pop-up
exportText = resultText.getText().toString().trim(); //.trim() removes space before and after text
if (!exportText.isEmpty()) {
saveToTxtFile(exportText);
} else {
Toast.makeText(SpeechToText.this, "Input field empty...", Toast.LENGTH_SHORT).show();
}
}
});
保存的方法:
private void saveToTxtFile(String mText) {
try {
File path = Environment.getExternalStorageDirectory(); //path to storage
File dir = new File(path + "/My App/Text Files/"); //create folders
dir.mkdirs();
String fileName = inputName + ".txt";
File file = new File(dir, fileName);
//FileWriter class is used to store characters in file
FileWriter fw = new FileWriter(file.getAbsolutePath());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(mText);
bw.close();
Toast.makeText(SpeechToText.this, "Saved successfully...", Toast.LENGTH_SHORT).show();
} catch(Exception e) {
//if anything goes wrong
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
对话框弹出方法:
public void openDialog() {
View view = (LayoutInflater.from(SpeechToText.this)).inflate(R.layout.user_input, null);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(SpeechToText.this);
alertBuilder.setView(view);
final EditText userInput = view.findViewById(R.id.userInput);
alertBuilder.setCancelable(true).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
inputName = String.valueOf(userInput.getText());
}
});
Dialog dialog = alertBuilder.create();
dialog.show();
}
在用户可以在对话框提示中输入文件名之前调用saveToTxtFile()
。
只需在onClick
Dialog 中调用它,inputName = String.valueOf(userInput.getText());
后侦听器进行空检查。此外,如果您更改文件保存方法会更好
saveToTxtFile(String mText)
自
saveToTxtFile(String mText, String filename)
这样,您将确定传递给此方法的文件名。
喜欢这个
alertBuilder.setCancelable(true).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
inputName = String.valueOf(userInput.getText());
if(inputName != "" && inputName != null)
saveToTxtFile(exportText,inputName);
else
//Toast an error here
}
});
同样在您的saveBtn
单击侦听器呼叫openDialog()
您在if (!exportText.isEmpty())
体内调用saveToTxtFile(exportText);
的位置。
喜欢这个:
exportText = resultText.getText().toString().trim(); //.trim() removes space before and after text
if (!exportText.isEmpty()) {
openDialog();
} else {
Toast.makeText(SpeechToText.this, "Input field empty...", Toast.LENGTH_SHORT).show();
}
这样,它只会在文件内容不为空时询问文件名。
你应该实现一个附加到你的对话框的侦听器,例如——
Dialog dialog;
dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
dialog.show();
TextView tv_message = (TextView) dialog .findViewById(R.id.textViewMessage);
tv_message.setText(message);
Button bt_yes = (Button)dialog.findViewById(R.id.buttonYes);
Button bt_no = (Button)dialog.findViewById(R.id.buttonNo);
bt_yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//HERE GOES YOUR CODE YOU WANT TO BE EXECUTED
// AFTER THE USER INOUTS ITS NAME AND HIT OK.
exportText = resultText.getText().toString().trim(); //.trim() removes space before and after text
if (!exportText.isEmpty()) {
saveToTxtFile(exportText);
} else {
Toast.makeText(SpeechToText.this, "Input field empty...", Toast.LENGTH_SHORT).show();
}
}
});
bt_no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
这是未经测试的,但你可以尝试这样的事情......
您只应在窃听saveBtn
时呼叫openDialog
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog(); //Calls for the dialog pop-up
}
});
然后,当将保存按钮添加到对话框中并在该点保存数据时。添加onShowListener
将阻止对话框关闭,直到您需要它。
public void openDialog() {
View view = (LayoutInflater.from(SpeechToText.this)).inflate(R.layout.user_input, null);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(SpeechToText.this);
alertBuilder.setView(view);
final EditText userInput = view.findViewById(R.id.userInput);
alertBuilder.setCancelable(true).setPositiveButton("Ok", null).setNegativeButton("Cancel", null);
Dialog dialog = alertBuilder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button deleteAllButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
deleteAllButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inputName = String.valueOf(userInput.getText());
exportText = resultText.getText().toString().trim(); //.trim() removes space before and after text
if (!exportText.isEmpty()) {
saveToTxtFile(exportText);
dialog.dismiss();
} else {
Toast.makeText(SpeechToText.this, "Input field empty...", Toast.LENGTH_SHORT).show();
}
}
});
Button cancelButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEUTRAL);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
}
});
dialog.show();
}