Jeżeli mam 5 radiobuttonow, ktore obsługą wyświetlanie daty w różnych formatach, i dwa przyciski do zmiany daty.
Czy jest możliwe, żeby po zmienieniu daty przyciskiem, wyświetlała się zmieniona data bez ponownego wybierania radiobuttona? Obecnie jak wciskam przycisk zmiany daty to muszę później naciskać na jeden z RadioButtonow aby data w TextView się zmieniła.
0
0
Wszystko sie da ale musisz pokazać jakiś kod.
0
Funkcja do wyświetlania formatu:
fun formatDate(format: Int): String {
if (format > 5 || format < 1)
throw FormatException()
when (format) {
1 -> return if (this.day < 10)
"0" + this.day + "." + this.month.number + "." + this.year
else
this.day.toString() + "." + this.month.number + "." + this.year
2 -> return Days.dayOfWeek(this.day, this.month.number, this.year) + "," + this.day + " " + this.month.name + " " + this.year
3 -> return this.day.toString() + " " + Months.getRoman(this.month.number) + " " + this.year
4 -> return this.day.toString() + " " + this.month.name + " " + this.year
5 -> return if (this.day < 10)
this.year.toString() + "-" + this.month.number + "-0" + this.day
else
this.year.toString() + "-" + this.month.number + "-" + this.day
else -> throw RuntimeException()
}
}
Funkcja zmieniajca date, np o tydzien w tył:
fun previousWeek() {
if ((this.day - 7) >= 1) {
this.day -= 7
} else {
if (this.month.number != 1) {
this.month.number -= 1
} else {
this.year -= 1
this.month = Months.getMonths(12)
}
this.day -= (7 - this.month.days)
}
}
klasa MainActivity
class MainActivity : AppCompatActivity() {
var date : Date = Date()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun show(v : View) {
prevButton.isEnabled = true
nextButton.isEnabled = true
defButton.isEnabled = true
fullButton.isEnabled = true
monthButton.isEnabled = true
isoButton.isEnabled = true
romanButton.isEnabled = true
var day : Int = dayText.text.toString().toInt()
var month : Int = monthText.text.toString().toInt()
var year : Int = yearText.text.toString().toInt()
date = Date(day, month, year)
}
fun nextWeek(v : View) {
date.nextWeek()
}
fun previousWeek(v : View) {
date.previousWeek()
}
fun format(v : View) {
if (defButton.isChecked) {
dateView.text = date.formatDate(1)
}
if (monthButton.isChecked) {
dateView.text = date.formatDate(4)
}
if (isoButton.isChecked) {
dateView.text = date.formatDate(5)
}
if (fullButton.isChecked) {
dateView.text = date.formatDate(2)
}
if (romanButton.isChecked) {
dateView.text = date.formatDate(3)
}
}
}
1
po kliknieciu np. w previousWeek zmieniasz date ale nie aktualizujesz widoku. dodaj jakis updateDateView() i tak pobierz aktualny format i wpisz go do pola textView.
albo zmodyfikuj swoja metode format tak zeby nie braka argumentu view (bo i tak go nie uzywasz) i wykonaj ja zaraz po previousWeek i nextWeek
1
fun nextWeek(v : View) {
date.nextWeek()
format()
}
fun previousWeek(v : View) {
date.previousWeek()
format()
}
ale ja bym to nazwal inaczej niz format, cos jak updateDateView()
0
Dziękuję : ) niepotrzebnie kombinowałem z robieniem z funkcji format zdarzenia