Android: RadioButton Trong Android | V1Study
Có thể bạn quan tâm
RadioButton có hai trạng thái: được chọn hoặc không được chọn. Điều này cho phép người dùng chọn một tùy chọn từ một bộ.
NÚT RADIO
Ví dụ
Ví dụ này sẽ minh họa cách tạo ứng dụng Android bằng cách sử dụng RadioButton kết hợp với RadioGroup.
Sau đây sẽ là nội dung của file activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/txtLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textColor="#009688" android:textSize="30sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.496" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.056"/> <ImageView android:id="@+id/logoV1Study" android:layout_width="170dp" android:layout_height="170dp" android:contentDescription="@string/logo_v1study" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.497" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.217" app:srcCompat="@drawable/logo_v1_regular"/> <RadioGroup android:id="@+id/rdoGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.515" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.587"> <RadioButton android:id="@+id/rdoPHP" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/php" android:textColor="#E91E63" android:textSize="24sp" android:textStyle="bold"/> <RadioButton android:id="@+id/rdoJavaAndroid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/java_android" android:textColor="#E91E63" android:textSize="24sp" android:textStyle="bold"/> <RadioButton android:id="@+id/rdoUnity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/unity" android:textColor="#E91E63" android:textSize="24sp" android:textStyle="bold"/> </RadioGroup> <Button android:id="@+id/btnClick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/click_me" android:textColor="#009688" android:textSize="30sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.496" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.726"/> </androidx.constraintlayout.widget.ConstraintLayout>Sau đây là nội dung của file MainActivity.java:
package v1study.com.radiobutton; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class MainActivity extends AppCompatActivity { RadioGroup rg1; RadioButton rb1; Button b1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listenerRadioButton(); } private void listenerRadioButton() { rg1 = (RadioGroup) findViewById(R.id.rdoGroup); b1 = (Button) findViewById(R.id.btnClick); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int idRB = rg1.getCheckedRadioButtonId(); rb1 = (RadioButton) findViewById(idRB); Toast.makeText(MainActivity.this, "Your select is: " + rb1.getText(), Toast.LENGTH_LONG).show(); } }); } }Sau đây sẽ là nội dung của file strings.xml:
<resources> <string name="app_name">RadioButtonV1Study</string> <string name="logo_v1study">Logo V1Study</string> <string name="php">PHP</string> <string name="click_me">Click Me</string> <string name="java_android">Java-Android</string> <string name="unity">Unity</string> </resources>Hãy thử chạy ứng dụng:
Nếu Người dùng chọn bất kỳ nút Radio nào thì text tương ứng của radio đó sẽ được hiện trên thông điệp Toast:
Từ khóa » Sử Dụng Radiobutton Trong Android
-
RadioButton Và RadioGroup Trong Lập Trình Android
-
Hướng Dẫn Và Ví Dụ Android RadioGroup Và RadioButton
-
Lập Trình Android - RadioButton - Hiệp Sĩ IT
-
Nút Chọn | Android Developers
-
Bài Tập 11: Sử Dụng Checkbox Và RadioButton Trong Android
-
AJ 09: RadioButton Và RadioGroup Trong Lập Trình Android - YouTube
-
Lập Trình Android A-Z – Bài 29: Radio Button - YouTube
-
Bài 7. CheckBox Và RadioButton - Tự Học Tin
-
RadioButton Và RadioGroup - Trần Ngọc Minh Notes
-
Lập Trình Android - Bài 7: Toast, CheckBox, RadioButton, Dialog
-
[AndroidBasic] Bài 10- RadioButton-RadioGroup - Nguyễn Khoa Ninh
-
Thêm Lề Giữa RadioButton Và Nhãn Của Nó Trong Android? - HelpEx
-
Android Nhận Giá Trị Từ Nút Radio đã Chọn - HelpEx
-
Radio Button Trong Lập Trình Android | Kênh Sinh Viên