开发者

How to implement a sequence of text input dialogs?

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) {
            doOpe开发者_JAVA百科ration3(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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜