Android:Dialog返回微调器来托管

时间:2012-10-16 15:24:50

标签: android android-dialog

所以我需要一个对话框,用户可以选择年/月(信用卡)。当用户单击“确定”到对话框时,应通知主机(即创建对话框的活动)有关更改。

我需要应用程序才能使用SDK版本7,因此我无法按照此处的建议使用DialogFragment:http://developer.android.com/guide/topics/ui/dialogs.html

目前这就是我所得到的:

public class DatePicker {

    /* The activity that creates an instance of this dialog fragment must
     * implement this interface in order to receive event callbacks.
     * Each method passes the DialogFragment in case the host needs to query it. */
    public interface DatePickerListener {
        public void onDialogPositiveClick(int month, int year);
        public void onDialogNegativeClick();
    }

    // Use this instance of the interface to deliver action events
    static DatePickerListener mListener;

    public static void show(Activity listner) {
        mListener = (DatePickerListener)listner;
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder((Activity)mListener);
        LayoutInflater inflater = listner.getLayoutInflater();

        builder.setView(inflater.inflate(R.layout.date_picker, null))
               .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {
                       mListener.onDialogPositiveClick(11, 11);
                   }
               })
               .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       dialog.cancel();
                   }
               }


            );  
        // Create the AlertDialog object and return it
        builder.create();
        builder.show();
    }

}

和XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp" >

    <Spinner
        android:id="@+id/spinnerMonth"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Spinner
        android:id="@+id/spinnerYear"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

所以我有两个问题:第一个是我似乎无法在onClick对话框中获得微调器值,其次我需要以编程方式设置微调器内容,我似乎无法弄清楚如何做到这一点(因为我似乎无法正确地获得对话框内容。)

有人能指出我正确的方向吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

首先,我在电话,计算机或任何地方看到的每一张信用卡实现都将所有元素放在一个窗口中。您确定要将用户最期望的体验更改为涉及弹出对话框的体验吗?

如果活动本身具有所有必要的输入:信用卡号码的文本字段,日期和月份的两个微调器以及CVS文本字段,那对你和他们来说都不会更简单吗?它不是好像即使在小屏幕移动设备上也会让你的空间不足。

无论你是否对自己的想法感到厌倦,答案都是一样的,如下所示:

class something {
  int mMonth;
  int mDay;
  (...)
monthSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
  @Override
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    mMonth = pos;   
  }
  @Override
  public void onNothingSelected(AdapterView<?> parent) { }
});