【问题标题】:get values from AlterDialog from 2 numberpickers and edittext从 2 个 numberpickers 和 edittext 从 AlterDialog 获取值
【发布时间】:2016-10-08 10:37:30
【问题描述】:

我想用 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();
}

}

【问题讨论】:

  • 你有什么错误吗?
  • 只需将活动上下文传递给您的对话框类。使用该上下文调用从警报对话框接收数据的方法。
  • 使用此代码没有错误,但没有任何反应。当我试图把例如。不幸的是,setText 或我能找到的任何解决方案大部分时间都会崩溃。

标签: android android-alertdialog numberpicker


【解决方案1】:

要将结果发送到 MainActivity,您必须遵循一个模式。使用单个方法onDialogOk(int value); 创建一个接口OnNumberSet。让您的 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 设为 final 并在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 2021-03-12
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多