2016-08-08 4 views
1

こんにちは、私はアンドロイドに新しく、問題に直面しています。私は多くを検索しましたが、DatePickerDialogボックスを使用してmを解決できませんでした。 。私は現在の時刻から1秒も引いてみました。 DatePickerはJanuary1990から開始しています。私は今日からそれを開始し、以前の日付をブロックしたい。ありがとう...DatePickerの最小日付を設定する

public void OnButtonClickDate() 

{ 
btn_date = (Button)findViewById(R.id.button); 

edit_date = (EditText)findViewById(R.id.editText); 

btn_date.setOnClickListener 
(

new View.OnClickListener() 
{ 
    @Override 
    public void onClick(View view) 
    { 
    DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() 
    { 
     @Override 
     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)                    
     view.setMinDate(System.currentTimeMillis()); 
     edit_date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year); 
     } 
    }, mYear, mMonth, mDay); 
    datePickerDialog.show(); 
    } 
} 
); 
} 
+0

whaあなたの書式では何が起こっていますか?これは読むのに苦労します:( – apelsoczi

+0

私は混乱してごめんなさい...フォーマットでコメントにして欲しいですか? –

+0

あなたは以前の投稿をフォーマットされたコードで編集する必要があります。 – apelsoczi

答えて

0

コードを調整してみてください。

btn_date = (Button) findViewById(R.id.button); 
edit_date = (EditText) findViewById(R.id.editText); 
DatePickerDialog datePickerDialog = new 
     DatePickerDialog(MainActivity.this, 
     new DatePickerDialog.OnDateSetListener() { 
      @Override 
      public void onDateSet(DatePicker view, int year, 
            int monthOfYear, int dayOfMonth) { 

       datePickerDialog.setMinDate(System.currentTimeMillis()); 
       mYear = year; 
       mMonth = monthOfYear; 
       mDay = dayOfMonth; 
       edit_date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year); 
      } 
     }, mYear, mMonth, mDay); 
datePickerDialog.setMinDate(System.currentTimeMillis()); 
btn_date.setOnClickListener(
     new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       datePickerDialog.show(); 
      } 
     } 
); 

Androidカレンダービューはバグのカスケードです。上記の方法が失敗した場合は、ピッカーからCalendarViewを削除してみてください。これらのバグare described here

datePicker.setCalendarViewShown(false); 

をバイパスする他の方法はまたねえ、あなたのアプリケーションでmuinimum日を設定するfolowingのサンプルコードを使用することができ、この機能

compile "com.wdullaer:materialdatetimepicker:2.4.0" 
+0

datePickerDialog.setMinDateは、私にDatePickerのビューオブジェクトを使用している理由を私に教えてくれます。 –

0

ためa libraryを使用してみてください:

Date date = new Date(); 

    final long todayInMillis = date.getTime(); 
    final long thirteenyearMillis = 409968000000L; 
    final long eightyYearMillis = 2522880000000L; 
    final long maxDate = todayInMillis - thirteenyearMillis; 
    final long minDate = todayInMillis - eightyYearMillis; 

上記のサンプルからアイデアを得ることを願っています...

+0

はい...ありがとうございます。このメソッドで作業してみましょう:-) –

関連する問題