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


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

OnCheckedChangeListener
Har bir alohida almashtirgichdagi bosishlarni boshqarishdan tashqari, biz odatda OnCheckedChangeListener tinglovchisini butun RadioGroup-ga uning kalitlari va ishlov berish tugmalari bilan osib qo'yishimiz mumkin. Buni amalga oshirish uchun kalitlarga belgilashdan android:onClick atributlarini olib tashlang va RadioGroup elementi uchun identifikatorni aniqlang:

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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kotlin" />


Keyinchalik, MainActivity kodida biz OnCheckedChangeListener tinglovchisini RadioGroup obyektiga osib qo'yamiz:


package com.example.viewapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// получаем объект RadioGroup
RadioGroup radGrp = (RadioGroup)findViewById(R.id.radios);
// обработка переключения состояния переключателя
radGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int id) {
TextView selection = findViewById(R.id.selection);
switch(id) {
case R.id.java:
selection.setText("Выбрана Java");
break;
case R.id.kotlin:
selection.setText("Выбран Kotlin");
break;
default:
break;
}
}});
}
}

RadioGroup.OnCheckedChangeListener tinglovchisi RadioGroup ob'ekti va tanlangan radio tugma identifikatori uzatiladigan onCheckedChanged() usulini belgilaydi. Keyinchalik, biz identifikatorni tekshirishimiz va ba'zi ishlov berishimiz mumkin.
Download 26.67 Kb.

Do'stlaringiz bilan baham:
1   2




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