Mobil ilovalarni yaratish texnologiyalari fanidan (amaliy) mashgʻulot Mavzu: 14. RadioButton bilan ishlash Qarshiboyev Akmal Radiobuttonlar


Download 26.67 Kb.
bet1/2
Sana04.04.2023
Hajmi26.67 Kb.
#1325341
  1   2
Bog'liq
adham mobil1


Mobil ilovalarni yaratish texnologiyalari fanidan (amaliy) mashgʻulot
Mavzu: 14.RadioButton bilan ishlash
Qarshiboyev Akmal
Radiobuttonlar RadioButton klassi tomonidan taqdim etilgan belgilash katakchalariga o'xshash funksiyalarni ta'minlaydi. Lekin tasdiqlash qutilaridan farqli o'laroq, biz radio tugmalar guruhida bir vaqtning o'zida faqat bitta radio tugmachani tanlashimiz mumkin.
Tanlash uchun radio tugmalar roʻyxatini yaratish uchun avvalo barcha radio tugmalarni oʻz ichiga olgan RadioGroup obyektini yarating:


xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

android:id="@+id/radios"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/selection"
>

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java"
android:onClick="onRadioButtonClicked"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kotlin"
android:onClick="onRadioButtonClicked"/>



RadioGroup klassi LinearLayout-dan olinganligi sababli, biz ro'yxatni vertikal yoki gorizontal yo'nalishga ham o'rnatishimiz mumkin, shu bilan birga nafaqat radio tugmachalarini, balki tugma yoki TextView kabi boshqa ob'ektlarni ham qo'shishimiz mumkin.
MainActivity sinfida biz radio tugmalarni tanlash bilan ishlashni aniqlaymiz:

package com.example.viewapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;


import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onRadioButtonClicked(View view) {
// если переключатель отмечен
boolean checked = ((RadioButton) view).isChecked();
TextView selection = findViewById(R.id.selection);
// Получаем нажатый переключатель
switch(view.getId()) {
case R.id.java:
if (checked){
selection.setText("Выбрана Java");
}
break;
case R.id.kotlin:
if (checked){
selection.setText("Выбран Kotlin");
}
break;
}
}
}



Download 26.67 Kb.

Do'stlaringiz bilan baham:
  1   2




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling