【问题标题】:How can I successfully return a string from a reusable Dialog in Android?如何从 Android 中的可重用对话框成功返回字符串?
【发布时间】:2012-09-11 11:03:49
【问题描述】:

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

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

另请注意,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, 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();
    }

}

【问题讨论】:

    标签: android android-edittext android-dialog


    【解决方案1】:

    像这样在你的类中实现接口,如下所示。您不需要将 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);
    

    【讨论】:

    • 这样做了,拓宽了我对对话框的理解。感谢您的快速回复!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    相关资源
    最近更新 更多