Android: ToggleButton trong Android
ToggleButton hiển thị các trạng thái checked/unchecked như một button. Về cơ bản, nó là một on/off button với một light indicator.
Thuộc tính của ToggleButton trong Android
Bảng dưới liệt kê một số thuộc tính quan trọng liên quan tới ToggleButton Control:
Thuộc tính | Miêu tả |
---|---|
android:disabledAlpha | Đây là alpha để áp dụng cho indicator khi bị vô hiệu hóa |
android:textOff | Đây là text cho nút khi nó không được checked |
android:textOn | Đây là text cho nút khi nó được checked |
Kế thừa từ lớp android.widget.TextView:
Thuộc tính | Miêu tả |
---|---|
android:autoText | Nếu được thiết lập, xác định rằng TextView này có một phương thức đầu vào thuần văn bản và tự động sửa các lỗi chính tả phổ biến |
android:drawableBottom | Đây là drawable để được vẽ dưới text |
android:drawableRight | Đây là drawable để được vẽ bên phải text |
android:editable | Nếu được thiết lập, xác định rằng TextView này có một phương thức đầu vào |
android:text | Đây là Text để hiển thị |
Kế thừa từ lớp android.view.View:
Thuộc tính | Miêu tả |
---|---|
android:background | Đây là một drawable để sử dụng như background |
android:contentDescription | Thuộc tính này định nghĩa text mà miêu tả ngắn gọn nội dung của view. |
android:id | Cung cấp một id cho view |
android:onClick | Đây là tên của phương thức trong context của View này, để triệu hồi khi view được click |
android:visibility | Điều khiển tính nhìn thấy của view |
Ví dụ
Ví dụ sau sẽ minh họa cách tạo ứng dụng Androidd với ToggleButton.
Bước | Miêu tả |
---|---|
1 | Bạn sử dụng Android Studio IDE để tạo một ứng dụng Android. |
2 | Sửa đổi file src/MainActivity.java để thêm một click event |
3 | Sửa đổi nội dung mặc định của file res/layout/activity_main.xml để chứa các UI Control |
4 | Chạy ứng dụng để chạy Android Emulator và kiểm tra kết quả các thay đổi đã thực hiện trong ứng dụng |
Sau đây là nội dung của file res/layout/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/textView" 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_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.072" /> <ImageView android:layout_width="170dp" android:layout_height="170dp" android:contentDescription="@string/logo_v1study" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.194" app:srcCompat="@drawable/logo_v1_regular" /> <ToggleButton android:id="@+id/toggleButtonON" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/on" android:textColor="#009688" android:textSize="30sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.17" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.555" /> <ToggleButton android:id="@+id/toggleButtonOff" 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.808" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.555" /> <Button android:id="@+id/btnClickMe" 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.498" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.722" /> </androidx.constraintlayout.widget.ConstraintLayout>
Sau đây là nội dung của file Main Activity:
package v1study.com.togglebutton; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import android.widget.ToggleButton; public class MainActivity extends AppCompatActivity { ToggleButton tg1,tg2; Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tg1=(ToggleButton)findViewById(R.id.toggleButtonON); tg2=(ToggleButton)findViewById(R.id.toggleButtonOff); btn=(Button)findViewById(R.id.btnClickMe); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String str=""; str+="The first ToggleButton status: "+tg1.getText(); str+="\nThe second ToggleButton status: "+tg2.getText(); Toast.makeText(MainActivity.this,str,Toast.LENGTH_LONG).show(); } }); } }
Sau đây là nội dung của file res/values/strings.xml:
<resources> <string name="app_name">ToggleButtonV1Study</string> <string name="logo_v1study">Logo V1Study</string> <string name="on">On</string> <string name="click_me">Click Me</string> </resources>
Chạy ứng dụng Android:
Nếu bạn nhấn vào các nút ToggleButton rồi nhấn nút Click Me thì sẽ nhận được thông báo trạng thái hiện tại của các ToggleButton: