如何实现一系列文本输入对话框?
我正在创建一个 Android 应用程序,我希望有一个函数可以依次创建两个文本输入对话框,然后根据值执行操作。因为android中的一切都是异步完成的,所以我想出了以下方法:
private void doOperation() {
final EditText input = new EditText(this);
showInput("Title", "Enter param1:", input, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
doOperation2(input.getText().toString());
}
});
}
private void doOperation2(String param1) {
if(/* Some condition on param1. */)
return;
final EditText input = new EditText(this);
showInput("Title", "Enter param2:", input, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
doOperation3(param1, input.getText().toString());
}
});
}
private void doOperation3(String param1, String param2) {
actuallyDoOperation();
}
private void showInput(String title, String message, final EditText input, DialogInterface.OnClickListener positiveActionListener) {
AlertDialog alert = new AlertDialog.Builder(this).create();
alert.setTitle(title);
alert.setMessage(message);
input.setInputType(InputType.TYPE_CLASS_TEXT);
alert.setView(input);
alert.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.ok), positiveActionListener);
alert.setButton(AlertDialog.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
}
这对我来说看起来有点混乱,有没有更好/更好/更干净/更正确的方法来做到这一点?
I am creating an android app and I would like to have a function that creates two text input dialogs one after the other and then performs an operation based on the values. Because everything in android is done asynchronously, I have come up with the following approach:
private void doOperation() {
final EditText input = new EditText(this);
showInput("Title", "Enter param1:", input, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
doOperation2(input.getText().toString());
}
});
}
private void doOperation2(String param1) {
if(/* Some condition on param1. */)
return;
final EditText input = new EditText(this);
showInput("Title", "Enter param2:", input, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
doOperation3(param1, input.getText().toString());
}
});
}
private void doOperation3(String param1, String param2) {
actuallyDoOperation();
}
private void showInput(String title, String message, final EditText input, DialogInterface.OnClickListener positiveActionListener) {
AlertDialog alert = new AlertDialog.Builder(this).create();
alert.setTitle(title);
alert.setMessage(message);
input.setInputType(InputType.TYPE_CLASS_TEXT);
alert.setView(input);
alert.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.ok), positiveActionListener);
alert.setButton(AlertDialog.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
}
This looks kind of messy to me, is there a better/nicer/cleaner/more correct way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论