Dialog插件phonegap编辑默认文本

时间:2014-07-17 16:19:20

标签: cordova phonegap-plugins cordova-3 cordova-plugins

我是编程并尝试使用phonegap创建Android应用程序的新手。 我正在使用cordova 3.5(Phonegap Dialog Plugin)中的对话框插件来启用提示,从用户那里获取一些值并将其存储在输入区域。

navigator.notification.prompt(
            'Enter Value : ',  // message
            onPrompt,                  // callback to invoke
            'Input',            // title
            ['Cancel','Options','Ok'],             // buttonLabels
            text               // defaultText
        )

默认文本是用户先前输入的值。如果我想纠正错字,我必须再次输入所有内容。我应该怎么做才能覆盖默认文本,我可以编辑当前值本身的默认文本?

找到此链接:http://twigstechtips.blogspot.in/2011/10/android-allow-user-to-editinput-text.html

所以,我尝试编辑这个文件:/plugins/org.apache.cordova.dialogs/src/android/Notifications.java

原始代码:

public synchronized void prompt(final String message, final String title, final JSONArray buttonLabels, final String defaultText, final CallbackContext callbackContext) {

    final CordovaInterface cordova = this.cordova;

    Runnable runnable = new Runnable() {
        public void run() {
            final EditText promptInput =  new EditText(cordova.getActivity());
            promptInput.setHint(defaultText);
            AlertDialog.Builder dlg = new AlertDialog.Builder(cordova.getActivity());
            dlg.setMessage(message);
            dlg.setTitle(title);
            dlg.setCancelable(true);

            dlg.setView(promptInput);

            final JSONObject result = new JSONObject();

            // First button
            if (buttonLabels.length() > 0) {
                try {
                    dlg.setNegativeButton(buttonLabels.getString(0),
                        new AlertDialog.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                try {
                                    result.put("buttonIndex",1);
                                    result.put("input1", promptInput.getText().toString().trim().length()==0 ? defaultText : promptInput.getText());                                            
                                } catch (JSONException e) { e.printStackTrace(); }
                                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
                            }
                        });
                } catch (JSONException e) { }
            }

            // Second button
            if (buttonLabels.length() > 1) {
                try {
                    dlg.setNeutralButton(buttonLabels.getString(1),
                        new AlertDialog.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                try {
                                    result.put("buttonIndex",2);
                                    result.put("input1", promptInput.getText().toString().trim().length()==0 ? defaultText : promptInput.getText());
                                } catch (JSONException e) { e.printStackTrace(); }
                                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
                            }
                        });
                } catch (JSONException e) { }
            }

            // Third button
            if (buttonLabels.length() > 2) {
                try {
                    dlg.setPositiveButton(buttonLabels.getString(2),
                        new AlertDialog.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                try {
                                    result.put("buttonIndex",3);
                                    result.put("input1", promptInput.getText().toString().trim().length()==0 ? defaultText : promptInput.getText());
                                } catch (JSONException e) { e.printStackTrace(); }
                                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
                            }
                        });
                } catch (JSONException e) { }
            }
            dlg.setOnCancelListener(new AlertDialog.OnCancelListener() {
                public void onCancel(DialogInterface dialog){
                    dialog.dismiss();
                    try {
                        result.put("buttonIndex",0);
                        result.put("input1", promptInput.getText().toString().trim().length()==0 ? defaultText : promptInput.getText());
                    } catch (JSONException e) { e.printStackTrace(); }
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
                }
            });

            dlg.create();
            dlg.show();

        };
    };
    this.cordova.getActivity().runOnUiThread(runnable);
}

做了一个改变:

          final EditText promptInput =  new EditText(this);
promptInput.setHint(defaultText);

和第二次改变:

 result.put("input1", promptInput.getText().toString().trim().length()==0 ? "" : promptInput.getText());                                            
                                } 

但是不起作用。无法从当前值编辑默认文本,所有内容都会被覆盖。

我需要做出哪些改变才能获得所需的功能?

2 个答案:

答案 0 :(得分:0)

我想我明白了。

您只需使用var defaultText声明window全局,并在onPrompt回调中指定输入的值。试试看是不是这样:

window.defaultText = "enter ur stuff";
        var showMyPrompt = function showPrompt() {                
                navigator.notification.prompt(
                'Please enter your name',  // message
                onPrompt,                  // callback to invoke
                'Registration',            // title
                ['Ok','Exit'],             // buttonLabels
                defaultText                 // defaultText
            );              
        };

     // process the promptation dialog result
function onPrompt(results) {
            alert("You selected button number " + results.buttonIndex + " and entered " + results.input1);
            defaultText = results.input1;
        }

因此,每当您再次点击提示时,每次添加到提示中的内容都会保留!无需高科技js编辑。

答案 1 :(得分:0)

我在Android上遇到了同样的问题而只是替换了

promptInput.setHint(defaultText);

通过

promptInput.setText(defaultText);