从2个numberpickers和edittext

时间:2016-06-08 08:56:38

标签: android alertdialog numberpicker

我想制作带有2个号码选择器的计时器来选择用户的开始时间,并使用editText输入计时器名称。我为此使用了自定义AlterDialog。 我遇到的问题是将numberpickers结果和用户输入的名称传递回MainActivity字段。 当我尝试自己学习编码时,我将非常感谢您的支持。 这是从MainActivity打开的MyDialog.java。

LayoutInflater inflater;
View v;
NumberPicker hourPicker;
NumberPicker minPicker;
EditText enterdish; 
TextView yourdishname;

@Override
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    inflater = getActivity().getLayoutInflater();
    v=inflater.inflate(R.layout.activity_dialog, null);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(v);

        hourPicker = (NumberPicker) v.findViewById(R.id.picker1_hours);
        hourPicker.setMaxValue(36);
        hourPicker.setMinValue(0);
        hourPicker.setWrapSelectorWheel(false);
        hourPicker.setOnValueChangedListener( new NumberPicker.
                OnValueChangeListener() {
                @Override
                public void onValueChange(NumberPicker picker, int
                    oldVal, int newVal) {

     }
          });
hourPicker.setFormatter(new NumberPicker.Formatter() {
              @Override
              public String format(int i) {
                   return String.format("%02d", i);
             }
           });

minPicker = (NumberPicker) v.findViewById(R.id.picker2_minutes);
minPicker .setMaxValue(59);
minPicker .setMinValue(1);
minPicker .setWrapSelectorWheel(false);
minPicker .setOnValueChangedListener( new NumberPicker.
        OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int
            oldVal, int newVal) {

        }
  });
minPicker.setFormatter(new NumberPicker.Formatter() {
      @Override
      public String format(int i) {
           return String.format("%02d", i);
     }
   });

    builder.setView(v).setPositiveButton("OK", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

    enterdish = (EditText) v.findViewById(R.id.editText1_dishname);
    yourdishname = (TextView)v.findViewById(R.id.textView1_dishnamemain);
            yourdishname.setText(enterdish.getText());
        }
    }).setNegativeButton("Cancel", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    });

    return builder.create();
}

}

1 个答案:

答案 0 :(得分:0)

要将结果发送到MainActivity,您必须遵循一种模式。使用单个方法OnNumberSet创建一个界面onDialogOk(int value);。让MainActivity实现此接口并覆盖此方法并使用该值执行任何操作,因为它将是NumberPicker中的值。

现在,在AlertDialog中(我认为它是从MainActivity启动的):

声明全局字段OnNumberSet mCallBack;;然后覆盖onAttach方法:

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        mCallBack= (OnNumberSet) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement onDialogOk");
    }
}

之后: 让你的NumberPicker最终并在onCreateDialog方法中声明它:

final NumberPicker minPicker = (NumberPicker) v.findViewById(R.id.picker2_minutes);

在positiveButton onClickListener中:

builder.setView(v).setPositiveButton("OK", new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        int numberPickerValue = minPicker.getValue();
        mCallBack.onDialogOk(numberPickerValue );
    }

如果您在同一个对话框中有Edittext,请声明EditText,找到它的视图,使其成为最终,并在正面按钮的同一onClick方法中,调用String text = editText.getText()。在这种情况下,您还必须将一个String参数添加到onDialogOk方法... Obs。该对话框应扩展DialogFragment