When choosing a year, a leap check is performed, but all values ​​are lined up by default to the first one entered, then when they change, nothing changes, I understand it is necessary to do something so that when changing the process starts and calculates again, as it is called.

Tell me maybe there is a video course good for Java, I feel floating.

public class MainActivity extends AppCompatActivity { public int maxDay = 31; public int maxYear = 2016; public int minYear = 1940; public int maxMonth = 12; public int minMonth = 1; public int minDay = 1; public int yearChoseInt; public int monthChoseInt = 0; public int modif = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); NumberPicker numberPicker3 = (NumberPicker) findViewById(R.id.numberPicker3); numberPicker3.setMaxValue(maxYear); numberPicker3.setMinValue(minYear); yearChoseInt = numberPicker3.getValue();//получаем значение года // yearChoose = (NumberPicker)findViewById(R.id.numberPicker3); if(yearChoseInt%4==0 && yearChoseInt%100 !=0 || yearChoseInt%400==0){ //проверка високосности года modif = modif+1; } else { modif = 0; } NumberPicker numberPicker2 = (NumberPicker) findViewById(R.id.numberPicker2); numberPicker2.setMaxValue(maxMonth); numberPicker2.setMinValue(minMonth); monthChoseInt = numberPicker2.getValue();//получаем значение месяца // monthChoose = (NumberPicker)findViewById(R.id.numberPicker2); monthChoseInt = numberPicker2.getValue();//получаем значение месяца switch (yearChoseInt) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: maxDay = 31; break; case 4: case 6: case 9: case 11: maxDay = 30; break; case 2: default: maxDay = 28 + modif; } NumberPicker numberPicker = (NumberPicker) findViewById(R.id.numberPicker); numberPicker.setMaxValue(maxDay); numberPicker.setMinValue(minDay); } 

}

    1 answer 1

    As I understand it, all the code is executed in the onCreate method, this method is executed when creating this activity only once. So that when the entered number changes, the function will work again, you need to make a button or process the data change signal in the input field and put the year's calculation code in the method responsible for pressing the button / changing the data in the input field.

    How to implement a method that reacts to a change in value: https://stackoverflow.com/questions/15983120/implementing-onvaluechange-to-a-numberpicker-in-android and place the processing of the year inside.

    Android documentation on NumberPicker: https://developer.android.com/reference/android/widget/NumberPicker.html

    • it turns out something like this - Alexey Fedorov
    • monthChoose.setOnValueChangedListener (new NumberPicker.OnValueChangeListener () {@Override public void onValueChange (NumberPicker picker, int oldVal, int newVal) {oldVal = minMonth; newVal = monthChoseInt; chyrCrCyntrCyChangedChangedChangedListher
    • Not only switch there, but also getValue. And if the year and month are taken from different fields, then for the field of the year, you should also make a method that will set the new value of yearChoseInt, and this variable itself should be made a class property or somehow transferred to the month check function - Shadasviar
    • And you have the opposite assignment there: oldVal = minMonth; newVal = monthChoseInt oldVal = minMonth; newVal = monthChoseInt and vice versa: minMonth = oldVal; monthChoseInt = newVal minMonth = oldVal; monthChoseInt = newVal , since the values ​​of oldVal and newVal are passed to your method from the field - Shadasviar
    • and if it is necessary to install new ones, then this is done through the methods setMin / MaxValue - Shadasviar