最近在用原生的控件和布局绘制一些界面并使用,虽然这些都是Android基本知识,但是有的时候真的感觉力不从心,感觉有必要对Android常用的控件和布局做一个系统的了解。后续一个月甚至更多的时间都会围绕这个主题展开,毕竟这里面还是有不少高级控件的,我也会尽量结合应用深入的进行了解。
今天,我们的主题是基础控件ToggleButton。在开始之前,我们还是以官方文档为开端来开始我们的讲解,下面是Android文档中对ToggleButton的简介:
public class ToggleButton extends CompoundButton
Displays checked/unchecked states as a button with a "light" indicator and by default accompanied with the text "ON" or "OFF".
这是一种特殊的有两种状态的继承至CompoundButton的控件,在不同的状态会有高亮显示效果,既然是继承至CompoundButton,那我们也随便看一下CompoundButton的介绍:
A button with two states, checked and unchecked. When the button is pressed or clicked, the state changes automatically.
基本上介绍和上面一样,那我们就来看看具体的用法,因为在上一篇关于Button的介绍中,我们使用了Constraintlayout布局操作有点复杂,感觉偏离了重心,那我们具体就直接使用Linearlayout来讲解。我们在布局代码中添加:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="aoto.com.commonwidgetandlayout.basic_widget.toggelButton.ToggelButtonActivity">
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ToggleButton" />
</LinearLayout>
运行的结果如图:
可见,ToggleButton默认的状态是“OFF”状态,在“ON”状态还会有一个高亮边框,那我们在平时的使用过程中,肯定不会直接使用文本“ON”和“OFF”,我们可以通过如下属性设置两种状态的文本:
<ToggleButton
android:id="@+id/toggleButton"
android:textOn="on状态"
android:textOff="off状态"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ToggleButton" />
再次运行之后,我们看一下运行结果:
同样的,我们可以监听不同的状态来设置不同的背景,来适配不同的使用场景,这个背景设置和Button中的一模一样。只是针对的状态不同而已,记得在Button中,我们的Drawable如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/colorAccent"></item>
<item android:state_pressed="false" android:drawable="@color/colorPrimary"></item>
</selector>
在我们的ToggleButton中,我们的背景Drawable如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/open"></item>
<item android:state_checked="false" android:drawable="@drawable/close"></item>
</selector>
可见,都是根据控件状态来切换照片,只是对应的状态不同罢了。运行的效果如下:
在上面我们也说了,这个控件通常对应两种状态,那么如何监听状态的变化了,我们可以通过其父类CompoundButton.OnCheckedChangeListener接口实现,具体代码如下:
//设置状态变化监听
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
}else {
}
}
});
这样,我们就可以在不同状态下处理不同的逻辑了。下面,我就举一个简单小例子,实现其作为一个灯的开关来实现灯的关闭状态切换。
在这个小例子中,我们使用了TransitionDrawable的知识(更多关于TransitionDrawable的知识请进入)。首先还是使用xml文件定义一个Drawable资源light_transition:
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/light_off">
</item>
<item android:drawable="@drawable/light_on">
</item>
</transition>
在布局中添加:
<ImageView
android:layout_marginTop="10dp"
android:id="@+id/light_view"
android:src="@drawable/light_transition"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
控制层ToggelButtonActivity.java:
package aoto.com.commonwidgetandlayout.basic_widget.toggelButton;
import android.graphics.drawable.TransitionDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ToggleButton;
import aoto.com.commonwidgetandlayout.R;
/**
* @author why
* @date 2019-5-7 20:22:25
*/
public class ToggleButtonActivity extends AppCompatActivity {
ToggleButton toggleButton;
TransitionDrawable drawable;
ImageView lightImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toggel_button);
toggleButton = findViewById(R.id.toggle_button);
lightImage = findViewById(R.id.light_view);
drawable = (TransitionDrawable) lightImage.getDrawable();
//设置状态变化监听
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
drawable.reverseTransition(1500);
} else {
drawable.reverseTransition(500);
}
}
});
}
}
上面我们结合ToggelButton的状态监听以及Drawable实现了一个应用的小示例。到这里,关于这个控件的介绍也算结束了。
关于其开源项目使用的较少,可以参考ToggleButton,其实现的效果和Switch特别类似,效果图如下:
注:欢迎扫码关注