2016-10-07 3 views
22

私は、ユーザーが日付(dd/mm/yyyy)を選択する必要があるアプリを開発しています。 ボタンクリックでDatePickerを使用してダイアログボックスを表示したいとします。 日付を一度選択すると、EditTextに表示される必要があります。ボタンクリックでDatePickerDialogを表示する方法は?

私は、Android Studioの2.2を使用している、分SDKを使用したプロジェクトは23

親切

必要なコードを実行するために私を助けています。

+1

こんにちはを表示、あなたはそれがあなたを助けることができるこのリンクhttp://stackoverflow.com/questionsに試すことができます/ 14933330/datepicker-how-to-popup-datepicker-when-click-on-edittext –

+1

[Datepickerの可能な複製:edittextをクリックしたときにdatepickerをポップアップする方法](http://stackoverflow.com/questions/14933330/datepicker -how-to-popup-datepicker-when-click-on-edittext) – poring91

+0

http://blog.nkdroidsolutions.com/android-custom-date-picker-dialog-example/ – duggu

答えて

3

次のコードが動作するのclickイベントリスナーであなたのダイアログ..

datePickerButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      showDialog(0); 
     } 
    }); 

@Override 
@Deprecated 
protected Dialog onCreateDialog(int id) { 
    return new DatePickerDialog(this, datePickerListener, year, month, day); 
} 

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { 
    public void onDateSet(DatePicker view, int selectedYear, 
          int selectedMonth, int selectedDay) { 
     day = selectedDay; 
     month = selectedMonth; 
     year = selectedYear; 
     datePickerButton.setText(selectedDay + "/" + (selectedMonth + 1) + "/" 
       + selectedYear); 
    } 
}; 
5
DatePickerDialog StartTime = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { 
      public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
       Calendar newDate = Calendar.getInstance(); 
       newDate.set(year, monthOfYear, dayOfMonth); 
       activitydate.setText(dateFormatter.format(newDate.getTime())); 
      } 

     }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH)); 

    btn_checkin.setOnClickListener(new View.OnClickListener() { 
              @Override 
              public void onClick(View v) { 
    StartTime .show(): 

}); 
7

私のために働きます。将来の選択時間を有効にするには、maxDateを削除する必要があります。あなたのbuild.gradleで

btnDate.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 
           DialogFragment newFragment = new DatePickerFragment(); 
            newFragment.show(getSupportFragmentManager(), "datePicker"); 
          } 
         }); 

      public static class DatePickerFragment extends DialogFragment 
         implements DatePickerDialog.OnDateSetListener { 

        @Override 
        public Dialog onCreateDialog(Bundle savedInstanceState) { 
         final Calendar c = Calendar.getInstance(); 
         int year = c.get(Calendar.YEAR); 
         int month = c.get(Calendar.MONTH); 
         int day = c.get(Calendar.DAY_OF_MONTH); 
         DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day); 
         dialog.getDatePicker().setMaxDate(c.getTimeInMillis()); 
         return dialog; 
        } 

        public void onDateSet(DatePicker view, int year, int month, int day) { 
         btnDate.setText(ConverterDate.ConvertDate(year, month + 1, day)); 
        } 
       } 
+1

いいね! ALLOW_PASTとは何ですか?ケーラー –

+0

あなたはそれを使用する必要はありません。それは私のためのものでした。これを使用することができます – Kayra

28

enter image description here

I.は時間24.2.1

dependencies { 
    compile 'com.android.support:appcompat-v7:X.X.X' 
    // where X.X.X version 
} 

IIで、最新のAPPCOMPATライブラリを追加します。あなたの活動をandroid.support.v7.app.AppCompatActivityに広げ、DatePickerDialog.OnDateSetListenerインターフェイスを実装してください。

public class MainActivity extends AppCompatActivity 
    implements DatePickerDialog.OnDateSetListener { 

III。 DatePickerDialogコンテキストを設定し、リスナの実装と日付ピッカーの開始年、月、日を設定します。

DatePickerDialog datePickerDialog = new DatePickerDialog( 
    context, MainActivity.this, startYear, starthMonth, startDay); 

IV。あなたのボタン

((Button) findViewById(R.id.myButton)) 
    .setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     datePickerDialog.show(); 
    } 
}); 
+0

素晴らしい!最初の2つのステップが完了しました。ステップIIIで何をする必要がありますか@Saulmm –

+0

必要なパラメータを入力してスニペットを挿入し、ボタンのリスナーから起動すると、日付選択ダイアログが表示されます。 – saulmm

関連する問題