如何从Android中的可重用对话框成功返回字符串?

时间:2012-09-11 02:33:56

标签: android android-edittext android-dialog

我没有使用EditTexts,而是选择使用可点击的类别来提示接受用户输入的Dialog。这个可重用的Dialog类存储在AllMethods.class中。最初的理论是我传递了一堆文本(用于描述,标题等)和目标TextView(用户在Dialog中输入的setText)。但是,在执行时,我在尝试执行TextView.setText(str)代码行时会得到nullpointer异常。

所以,我的问题:如何在用户点击确定按钮后成功更改textView或传回字符串?

另请注意,TextView已声明并已实例化。我假设nullpointer是因为它在远程类中。这是代码:

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class AllMethods {


    //fieldRequest is a reusable prompt to get a user's input for a field without taking up so much space
    /* Legend of inputs:
     * requestingActivity=the activity which is prompting the popup
     * dialogMessage = the description of the editText being requested
     * dialogTitle = the title to be displayed on the dialogbox
     * editTextHint = the Hint attribute that will give the user an example of the expected input
     * inputType = numeric, text, phone number, etc
     */



    public static void fieldRequest(Activity activity, String dialogMessage, String dialogTitle, final TextView outputText, int inputType, String optionalFieldSuffix){
        final Dialog dialog= new Dialog(activity);
        dialog.setContentView(R.layout.dialog_layout);
        dialog.setTitle(dialogTitle);
        //If the message/description exists, put it in
        if(dialogMessage!=null||dialogMessage!=""){TextView description = (TextView) dialog.findViewById(R.id.dialog_description);
        description.setText(dialogMessage);
        }
        //Identify the editText, set the input type appropriately and fill in the hint if applicable
        final EditText inputField = (EditText) dialog.findViewById(R.id.dialog_inputbox);
        if(Integer.toString(inputType)!=null){inputField.setInputType(inputType);}

        if(optionalFieldSuffix!=null){TextView suffix = (TextView) dialog.findViewById(R.id.dialog_optionalFieldSuffix);            
        suffix.setText(optionalFieldSuffix);}

        ImageButton dialogCancel = (ImageButton) dialog.findViewById(R.id.dialog_cancel);
        ImageButton dialogDone = (ImageButton) dialog.findViewById(R.id.dialog_done);
        dialogCancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                dialog.dismiss();

            }
        });

        dialogDone.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                outputText.setText(inputField.getText().toString());
                dialog.dismiss();

            }
        });

        dialog.show();
    }

}

1 个答案:

答案 0 :(得分:0)

像这样在你的类中实现接口,如下所示。您无需将textview作为此函数的输入传递。

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class AllMethods {


    //fieldRequest is a reusable prompt to get a user's input for a field without taking up so much space
    /* Legend of inputs:
     * requestingActivity=the activity which is prompting the popup
     * dialogMessage = the description of the editText being requested
     * dialogTitle = the title to be displayed on the dialogbox
     * editTextHint = the Hint attribute that will give the user an example of the expected input
     * inputType = numeric, text, phone number, etc
     */



    public static void fieldRequest(Activity activity, String dialogMessage, String dialogTitle, int inputType, String optionalFieldSuffix, final DialogResponse dr){
        final Dialog dialog= new Dialog(activity);
        final DialogResponse dialogResponse = dr;
        dialog.setContentView(R.layout.dialog_layout);
        dialog.setTitle(dialogTitle);
        //If the message/description exists, put it in
        if(dialogMessage!=null||dialogMessage!=""){TextView description = (TextView) dialog.findViewById(R.id.dialog_description);
        description.setText(dialogMessage);
        }
        //Identify the editText, set the input type appropriately and fill in the hint if applicable
        final EditText inputField = (EditText) dialog.findViewById(R.id.dialog_inputbox);
        if(Integer.toString(inputType)!=null){inputField.setInputType(inputType);}

        if(optionalFieldSuffix!=null){TextView suffix = (TextView) dialog.findViewById(R.id.dialog_optionalFieldSuffix);            
        suffix.setText(optionalFieldSuffix);}

        ImageButton dialogCancel = (ImageButton) dialog.findViewById(R.id.dialog_cancel);
        ImageButton dialogDone = (ImageButton) dialog.findViewById(R.id.dialog_done);
        dialogCancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialogResponse.actionNo();
                dialog.dismiss();

            }
        });

        dialogDone.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialogResponse.actionYes(inputField.getText().toString());
                dialog.dismiss();
            }
        });

        dialog.show();
    }

    public interface DialogResponse {
        public void actionYes(String str);

        public void actionNo();
    }


}

<强>用法:

DialogUtility.DialogResponse dr = new DialogResponse() {

            @Override
            public void actionYes(String str) {
                // TODO Auto-generated method stub

                 **// your code to set your string to textview**
            }



            @Override
            public void actionNo() {

            }
        };
        **//Make sure you modify code to match your variables**
         AllMethods
                .fieldRequest(activity, dialogMessage, dialogTitle, inputType, optionalFieldSuffix, dr);