状态开关(ToggleButton)

状态开关(ToggleButton):

常用属性:isChecked(是否被选中,如true)

监听:1.监听方法:setOnCheckedChangeListener

   2.监听器:CompoundButton.OnCheckedChangeListener

1.Activity

//状态的开关
public class ToggleButtonActivity extends Activity {

    private ToggleButton toggleButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.toggle_button);

        toggleButton = (ToggleButton)findViewById(R.id.toggleButtonId);

        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(ToggleButtonActivity.this, "无线网络打开了!", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(ToggleButtonActivity.this, "无线网络关闭了!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

2.xml文件

<?xml version="1.0" encoding="utf-8"?>
<!-- 状态的开关页面 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="5dp" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="无线网:"
        android:textSize="20sp" />

    <ToggleButton
        android:id="@+id/toggleButtonId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

3.效果显示图

时间: 2024-08-04 20:44:07

状态开关(ToggleButton)的相关文章

状态开关(ToggleButton)

用一个开关实现两种状态,一个true执行一个命令,一个false执行一个命令. 先学习自慕课网. 先进行activity_main改写. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pa

Android——图片视图(ImageView)、状态开关按钮(ToggleButton)、时钟

xml <?xml version="1.0" encoding="utf-8"?> <!--滚动视图--> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_pare

安卓学习第8课——开关ToggleButton、Switch

今天学的是对开关的监听,两种开关ToggleButton和switch <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="ma

使用Scroller制作滑块开关ToggleButton

Scroller这个类在自己定义view中使用的还算是非常频繁的,和它名字一样.我们通常是在控制滑动的时候使用Scroller,以便让view滑动起来不那么生硬.在官方的解释上,Scroller是一个滑动辅助类,也就是说Scroller本身并不參与滑动,而是让我们的代码在Scroller的辅助下轻松的实现平滑滑动的效果. 既然Scroller仅仅是一个辅助类,那能不能利用它来辅助一些其它的功能呢? 当然能够,今天带来额Toggle就是利用Scroller来实现的一个平滑的开关button. 一.

Android 多状态按钮 ToggleButton

ToggleButton 选中状态,未选中状态并且需要为不同的状态设置不同的显示文本. 属性: checked="true" textOff="" textOn="" private ToggleButton tb ; private ImageView iv ; tb=(ToggleButton) findViewById(R.id.toggleButton1); iv=(ImageView) findViewById(R.id. imageV

状态开关按钮(ToggleButton)

      Demo2\togglebutton_demo\src\main\res\layout\activity_main.xml 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_paren

Android——图片视图(ImageView)、状态开关按钮(ToggleButton)、时钟、图片透明度、滚动和时间选择器

activity_ui1.xml dth="wrap_content" android:layout_height="wrap_content" android:textOn=" " android:textOff=" " android:background="@drawable/qq" android:id="@+id/tob1"/> <AnalogClock andro

MAC OSX通过Terminal命令行控制蓝牙状态开关

defaults write /Library/Preferences/com.apple.Bluetooth.plist ControllerPowerState 0 #关闭蓝牙服务 defaults write /Library/Preferences/com.apple.Bluetooth.plist ControllerPowerState 1 #打开蓝牙服务 killall blued #结束所有蓝牙服务进程 launchctl unload /System/Library/Launc

Android基础UI篇------TextView及其子类

一.TextView 继承自View类,本质上就是一个文本编辑器,只是Android关闭它的编辑功能.把编辑文字的功能交给了它的子类--EditText. 特殊之处: 1.通过设置ellipsize=marquee,可以实现"跑马灯" android:width="wrap_content" android:focusable = "true" android:singleline = "true" android:ellip