3

次の方法を使用してダイアログをポップアップし、日付を選択しています。

private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                dobYear = year;
                dobMonth = monthOfYear;
                dobDay = dayOfMonth;
                if(isEighteenYearOld()){
                   //display the current date
                    dateDisplay();
                } else{
                   Toast.makeText(mContext, "You must be 18 year old", Toast.LENGTH_SHORT).show();
                }  
            } 

        };

選択した日付を onDateSet で取得できることはわかっています。しかし、私がしようとしているのは、選択した日付が18歳未満の場合、ユーザーに警告する必要があるということです. 上記のコードを試しましたが、ダイアログが閉じてアクティビティに戻りました。

ユーザーが 18 歳の日付を選択するまでダイアログを表示したいのですが、ダイアログで onclick イベントを取得する方法がわかりません。

4

2 に答える 2

6

使用している Date Picker Dialog は廃止されているため、使用しないことをお勧めします。

日付ピッカーは2つの方法で実装できます(私が知っている)

  1. ダイアログフラグメントの使用
  2. AlertDialog の使用

ダイアログフラグメントの使用:

public class MainActivity extends FragmentActivity {
    EditText text;
    Button b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b=(Button)findViewById(R.id.button1);
        text=(EditText)findViewById(R.id.editText1);
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                DateDialogFragment datepicker=new DateDialogFragment();
                datepicker.show(getSupportFragmentManager(), "showDate");
            }
        });
    }

    public class DateDialogFragment extends DialogFragment  implements DatePickerDialog.OnDateSetListener{

        public DateDialogFragment()
        {
        }
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            Calendar cal=Calendar.getInstance();
            int year=cal.get(Calendar.YEAR);
            int month=cal.get(Calendar.MONTH);
            int day=cal.get(Calendar.DAY_OF_MONTH);
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            showSetDate(year,monthOfYear,dayOfMonth);
        }

        }

    public void showSetDate(int year,int month,int day) {
    text.setText(year+"/+"+month+"/"+day);
    }
}

このサンプルを確認して、アクティビティに同じものを実装してください。

警告ダイアログの使用:

2番目のものを使用するのは非常に簡単です

res/layout フォルダーにレイアウトを作成し、レイアウトに DatePicker を配置します

 LayoutInflater  inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = (View) inflater.inflate(R.layout.yourlayout, null);
DatePicker picker=(DatePicker)view.findViewById(R.id.datepicker);

  AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
 builder.setView(view).
        builder.setMessage(R.string.dialog_fire_missiles)
               .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
于 2013-02-22T18:41:22.600 に答える
1

このようなダイアログのカスタム実装を使用したくない場合は、DatePickerDialogそのような動作を実現するためにサブクラス化する必要があります。だけでダイアログが閉じるのをブロックすることはできませんDatePickerDialog.OnDateSetListener

残念ながら、ダイアログの実装は API レベルによって異なるため、サブクラス化で目的の動作を実現するのは簡単ではありません。確実に機能させるには、いくつかのハックを追加する必要があります。

適切な日付が設定されていない限り (またはキャンセルまたは戻るボタンが押されていなければ)、ダイアログが閉じないようにするサンプル実装を作成しました。ユーザーにアラートを表示するように調整します。最適な場所は、onClick()メソッドの else ブランチです。

class CheckingDatePickerDialog extends DatePickerDialog {

    private int year;
    private boolean cancel = false;
    private boolean isCancelable = true;

    CheckingDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
        super(context, callBack, year, monthOfYear, dayOfMonth);
        this.year = year;
    }

    CheckingDatePickerDialog(Context context, int theme, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
        super(context, theme, callBack, year, monthOfYear, dayOfMonth);
        this.year = year;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // allow closing the dialog with cancel button
        Button btn = getButton(BUTTON_NEGATIVE);
        if (btn != null) {
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancel = true;
                    dismiss();
                }
            });
        }
    }

    @Override
    public void setCancelable(boolean flag) {
        isCancelable = false;
        super.setCancelable(flag);
    }

    @Override
    public void onBackPressed() {
        // allow closing the dialog with back button if the dialog is cancelable
        cancel = isCancelable;
        super.onBackPressed();
    }

    private boolean isOldEnough() {
        // test if the date is allowed
        return year <= 1994;
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // necessary for some Honeycomb devices
            DatePicker dp = getDatePicker();
            this.year = dp.getYear();
        }

        if (isOldEnough()) {
            // OnDateSetListener is called in super.onClick()
            super.onClick(dialog, which);  
        } else {
            // place your alert here
        }
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        // on some Honeycomb devices called only with the first change
        // necessary for devices running Android 2.x
        this.year = year;
        super.onDateChanged(view, year, month, day);
    }

    @Override
    public void dismiss() {
        if (cancel || isOldEnough()) {
            // do not allow the dialog to be dismissed unless a cancel or back button was clicked
            super.dismiss();
        }
    }
};
于 2013-02-22T17:29:21.247 に答える