将用户输入从警报对话框传递给活动,并在对话框在Android中解散时处理它?

时间:2013-06-21 01:26:52

标签: android android-dialog android-dialogfragment

我有一个弹出对话框,询问用户输入。

一旦用户输入他/她的输入然后单击“确定”,我希望片段将用户输入传递回对话框后面的活动。

我读过有关onDismiss(DialogInterface dialog)的内容。但是,作为Java和Android的新学习者,我无法正确实现它:

  1. 处理程序永远不会在我的代码中运行。

  2. 即使在处理程序运行之后,我仍然不知道如何从参数dialog获取用户输入,因为它是DialogInterface类型,我不知道如何处理。

  3. 以下是我的代码段。

    活动方:

        public class CalibrationActivity extends Activity implements SensorEventListener, DialogInterface.OnDismissListener {
    
        ...
    
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_calibration);
    
                buttonInit();
                sensorInit();
            }
    
    
            private void buttonInit() {
    
                // start calibrating button
                startCalibratingButton = (Button) findViewById(R.id.button1);
                startCalibratingButton.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
    
                        startCalibrationFlag = true;
    
                        Toast.makeText(CalibrationActivity.this, "Please walk " + Constant.CALIBRATION_STEP_NUMBER + " steps and press the 'Done Calibrating' button", Toast.LENGTH_SHORT).show();
                        startCalibratingButton.setEnabled(false);
                        doneCalibratingButton.setEnabled(true);
                    }
                });
    
                // done calibrating button
                doneCalibratingButton = (Button) findViewById(R.id.button2);
                doneCalibratingButton.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
    
                        startCalibrationFlag = false;
    
                        RequireDistanceDialogFragment requireDistanceDialogFragment = new RequireDistanceDialogFragment();
                        requireDistanceDialogFragment.setCancelable(false);
                        requireDistanceDialogFragment.show(getFragmentManager(), ALARM_SERVICE); // dialog pops up that leads the new user to calibrate
    
                    // Error Here: "The method setOnDismissListener(new OnDismissListener(){}) is undefined for the type RequireDistanceDialogFragment"
                        requireDistanceDialogFragment.setOnDismissListener(new OnDismissListener() {
    
                            public void onDismiss(DialogInterface dialog) {
                                LoadingActivity.this.finish();
                            }
                        });
    
                    }
                });
    
                startCalibratingButton.setEnabled(true);
                doneCalibratingButton.setEnabled(false);
            }
    
            // called when the dialog is dismissed
            public void onDismiss(DialogInterface requireDistanceDialogFragment) {
    
    // Error Here: The method getDistanceEntered() is undefined for the type DialogInterface
                    double userInput = requireDistanceDialogFragment.getDistanceEntered();
                    processTheUserInputHere();
    
            }
    
        }
    

    对话方:

    package com.example.drsystem;
    
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.app.DialogFragment;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.InputType;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class RequireDistanceDialogFragment extends DialogFragment {
    
        private Editable distance;
        private boolean distanceIsEntered = false;
    
        public Dialog onCreateDialog(Bundle savedInstanceState) {
    
            // Use the Builder class for convenient dialog construction
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            final EditText input = new EditText(getActivity());
            input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    
            builder.setTitle("Distance is required to calibrate the stride length estimation.").setMessage("Distance (in m) for these " + Constant.CALIBRATION_STEP_NUMBER + " steps?").setView(input).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    
                public void onClick(DialogInterface dialog, int whichButton) { // click ok
    
                    distanceIsEntered = true;
                    distance = input.getText();
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    
                public void onClick(DialogInterface dialog, int whichButton) { // click cancel
    
                    Toast.makeText(getActivity(), "Calibration not done", Toast.LENGTH_SHORT).show();
                }
            });
    
            // Create the AlertDialog object and return it
            return builder.create();
        }
    
        // if the user has not entered the distance yet, the distance returned is -1
        public double getDistanceEntered() {
    
            if (distanceIsEntered)
                return Double.parseDouble(distance.toString());
            else
                return -1;
        }
    
    }
    

    您可以通过解决我遇到的两个问题来帮助我修改

    或者您可以提出新的方法来实现这一目标。

1 个答案:

答案 0 :(得分:1)

对话框中的onClickMethod应将输入放在消息中

  

builder.setMessage("来自用户的消息");

你的onDismiss监听器应该在对话之前从对话框中获取数据

  

public void onDismiss(DialogInterface dialog){   String messageFromUser = dialog.getMessage("你想要的字符串");                               LoadingActivity.this.finish();                           }

为什么要完成装载活动?