Android控件ToggleButton和Switch

  • 基本概念
  • ToggleButton
    • 添加控件
    • 监听事件
    • 结果展示
  • Switch

基本概念

ToggleButton,即切换按钮,允许用户改变两个状态之间的设置。基本类型如左图,在Android4.0(API14)之后介绍了另一种被称为Switch的开关按钮提供了一个滑块控件,如右图。
ToggleButton Switch
ToggleButton和Switch是CompoundButton的子类。

ToggleButton

ToggleButton使用流程如下:

添加控件

在xml文件中添加ToggleButton,代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ToggleButton  android:id="@+id/tb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="开" android:textOff="关" android:onClick="onToggleButtonClick" />
    <ToggleButton  android:id="@+id/tb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tb1" android:textOn="开" android:textOff="关" />
</RelativeLayout>

其中,属性android:textOn和属性android:textOff用来设置切换按钮文本,属性android:onClick为绑定的事件方法。

监听事件

activity类中绑定监听事件,代码如下:

package com.sywyg.togglebutton_switch_test;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

/**ToggleButton和Switch练习 * @author sywyg * @time 2015/4/9 */

public class MainActivity extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener {
    private ToggleButton toggleButton;
    private ToggleButton toggleButton2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toggleButton = (ToggleButton)findViewById(R.id.tb1);
        toggleButton2 = (ToggleButton)findViewById(R.id.tb2);
        toggleButton2.setOnCheckedChangeListener(this);

    }

    public void onToggleButtonClick(View view){
        boolean on = ((ToggleButton)view).isChecked();
        if(on){
            Toast.makeText(this,"开关已开",Toast.LENGTH_SHORT).show();
        }else
            Toast.makeText(this,"开关已关",Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onCheckedChanged(CompoundButton compoundButton,boolean isChecked){
        if(isChecked){
            Toast.makeText(this,"开关改成已开",Toast.LENGTH_SHORT).show();
        }else
            Toast.makeText(this,"开关改成已关",Toast.LENGTH_SHORT).show();
    }
}

这里实现了两种事件监听,一种是通过ToggleButton的属性android:onClick绑定;另一种是监听开关是否改变,通过实现接口CompoundButton.OnCheckedChangeListener。说法不一样但是都能达到相同的结果。

结果展示

如图所示,两种事件监听效果:

Switch

Switch和ToggleButton无论是xml添加还是activity类中监听(也是两种监听)都一样这里直接给出代码,不过多解释。

 <Switch  android:id="@+id/switch1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tb2" android:textOn="开" android:textOff="关" android:onClick="onSwitchClicked" />
package com.sywyg.togglebutton_switch_test;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;

/**ToggleButton和Switch练习 * @author sywyg * @time 2015/4/9 */

public class MainActivity extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener {
    private ToggleButton toggleButton;
    private ToggleButton toggleButton2;
    private Switch aSwitch;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toggleButton = (ToggleButton)findViewById(R.id.tb1);
        toggleButton2 = (ToggleButton)findViewById(R.id.tb2);
        toggleButton2.setOnCheckedChangeListener(this);
        aSwitch = (Switch)findViewById(R.id.switch1);

    }
    public void onSwitchClicked(View view){
        boolean on = ((Switch)view).isChecked();
        if(on){
            Toast.makeText(this,"开关已开",Toast.LENGTH_SHORT).show();
        }else
            Toast.makeText(this,"开关已关",Toast.LENGTH_SHORT).show();
    }
    public void onToggleButtonClick(View view){
        boolean on = ((ToggleButton)view).isChecked();
        if(on){
            Toast.makeText(this,"开关已开",Toast.LENGTH_SHORT).show();
        }else
            Toast.makeText(this,"开关已关",Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onCheckedChanged(CompoundButton compoundButton,boolean isChecked){
        if(isChecked){
            Toast.makeText(this,"开关改成已开",Toast.LENGTH_SHORT).show();
        }else
            Toast.makeText(this,"开关改成已关",Toast.LENGTH_SHORT).show();
    }
}

结果演示:

可以看到属性android:textOn和属性android:textOff不再起作用。

你可能感兴趣的:(android)