Android开发学习笔记:浅谈ToggleButton
16lz
2021-01-23
ToggleButton(开关按钮)是Android系统中比较简单的一个组件,是一个具有选中和未选择状态双状态的按钮,并且需要为不同的状态设置不同的显示文本。
ToggleButton常用的XML属性
属性名称 | 描述 |
android:disabledAlpha | 设置按钮在禁用时透明度。
|
android:textOff | 未选中时按钮的文本 |
android:textOn | 选中时按钮的文本 |
下面是具体的例子:
第一个例子是通过Toast显示ToggleButton不同的状态时的信息
MainActivity.java
- package com.android.togglebutton;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Toast;
- import android.widget.ToggleButton;
- public class MainActivity extends Activity {
- //声明ToggleButton
- private ToggleButton togglebutton;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- togglebutton = (ToggleButton) findViewById(R.id.togglebutton);
- togglebutton.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- // 当按钮第一次被点击时候响应的事件
- if (togglebutton.isChecked()) {
- Toast.makeText(MainActivity.this, "你喜欢球类运动", Toast.LENGTH_SHORT).show();
- }
- // 当按钮再次被点击时候响应的事件
- else {
- Toast.makeText(MainActivity.this, "你不喜欢球类运动", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <ToggleButton
- android:id="@+id/togglebutton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textOn="喜欢"
- android:textOff="不喜欢"
- />
- LinearLayout>
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">你喜不喜欢球类运动?string>
- <string name="app_name">测试ToggleButtonstring>
- resources>
效果图:
第二个例子通过图片的变化显示ToggleButton不同的状态时的图片
MainActivity.java
- package com.android.togglebutton;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.CompoundButton;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- import android.widget.ImageView;
- import android.widget.ToggleButton;
- public class MainActivity extends Activity {
- //声明ImageView,ToggleButton
- private ImageView p_w_picpathView;
- private ToggleButton toggleButton;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //通过findViewById获得ImageView,ToggleButton
- p_w_picpathView=(ImageView) findViewById(R.id.p_w_picpathView);
- toggleButton=(ToggleButton)findViewById(R.id.toggleButton);
- toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener(){
- public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
- toggleButton.setChecked(isChecked);
- //使用三目运算符来响应按钮变换的事件
- p_w_picpathView.setImageResource(isChecked?R.drawable.pic_on:R.drawable.pic_off);
- }
- });
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageView
- android:id="@+id/p_w_picpathView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/pic_off"
- android:layout_gravity="center_horizontal"
- />
- <ToggleButton
- android:id="@+id/toggleButton"
- android:layout_width="130dip"
- android:layout_height="wrap_content"
- android:textOn="开灯"
- android:textOff="关灯"
- android:layout_gravity="center_horizontal"
- />
- LinearLayout>
效果图:
更多相关文章
- Android控件之利用selector自定义的带文字的图片按钮
- Android 8.0 SystemUI下拉状态栏快捷开关
- Android的按钮监听事件&自定义回调函数
- Android Alert Dialog解决点击按钮对话框不关闭的问题
- Android使用selector自定义按钮
- Android Nine Patch图片及按钮背景
- Android 基本按钮
- Android 应用程序窗体显示状态操作(requestWindowFeature()的应用
- Android中控件的显示和隐藏以及EditText的可编辑和不可编辑状态