Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

Radio Group ใน Android คืออะไร?


ก่อนจะยกตัวอย่างมาควรรู้ก่อนว่า Radio Group ใน Android คืออะไร Radio Group ประกอบด้วยกลุ่มปุ่มตัวเลือก การใช้ปุ่มตัวเลือกทำให้เราสามารถเลือกและยกเลิกการเลือกได้ตามความต้องการของผู้ใช้

ตัวอย่างนี้สาธิตวิธีใช้กลุ่มวิทยุใน Android

ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่

ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:app="https://schemas.android.com/apk/res-auto"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center"
   android:orientation="vertical"
   tools:context=".MainActivity">
   <TextView
      android:id="@+id/result"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Select your favourite subject"
      android:textSize="20sp"
      android:padding="10dp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent" />
      <RadioGroup
         android:id="@+id/radioGroup"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:padding="10dp">
      <RadioButton
         android:id="@+id/adroid"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Android"/>
      <RadioButton
         android:id="@+id/java"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="java"/>
      <RadioButton
         android:id="@+id/cpp"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="CPP"/>
      <RadioButton
         android:id="@+id/clan"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="C Programming"/>
      </RadioGroup>
   <Button
      android:id="@+id/buton"
      android:text="ok"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
</LinearLayout>

ในโค้ดด้านบนนี้มีปุ่มตัวเลือกสี่ปุ่ม เมื่อผู้ใช้คลิกที่ปุ่ม จะได้รับข้อความของปุ่มตัวเลือกที่ตรวจสอบแล้ว

ขั้นตอนที่ 3 − เพิ่มรหัสต่อไปนี้ใน src/MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.util.Linkify;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
   RadioButton radioButton;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final Button button=findViewById(R.id.buton);
      final RadioGroup radioGroup=findViewById(R.id.radioGroup);
      button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            int clickedRadioButton=radioGroup.getCheckedRadioButtonId();
            radioButton=findViewById(clickedRadioButton);
            if(clickedRadioButton ==-1){
               button.setError("Error");
            } else {
               button.setError(null);
               Toast.makeText(MainActivity.this,radioButton.getText(),Toast.LENGTH_LONG).show();
            }
         }
      });
   }
}

ขั้นตอนที่ 5 ไม่จำเป็นต้องเปลี่ยน manifest.xml มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก แล้วตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ -

Radio Group ใน Android คืออะไร?

เลือกหัวเรื่องใด ๆ ดังตัวอย่างที่เราได้เลือกจาวาดังที่แสดงด้านล่าง -

Radio Group ใน Android คืออะไร?

ตอนนี้คลิกที่ปุ่มตกลงเพื่อรับข้อความของปุ่มตัวเลือกที่เลือกจากกลุ่มวิทยุ

Radio Group ใน Android คืออะไร?

ในผลลัพธ์ข้างต้น เราได้เลือกปุ่ม ตกลง มันจะแสดงข้อความโทสต์ของข้อความปุ่มตัวเลือกที่เลือก