我想为我的应用程序实现一个三向滑动按钮。在第一个位置它应该显示一种颜色,在中心它应该显示另一种颜色,在结束位置它应该再次改变颜色。在这里,我给出了这些图像。



我如何实现这一点?
发布于 2018-10-16 01:43:08
给你,我根据自己的需要开发了它,我正在分享代码。非常容易做和配置。只需使用switch语句查找指针位置并执行某些操作即可。
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/md_green_100"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="@string/send"
android:textColor="@color/colorPrimaryDark"
android:textSize="18sp"
android:textStyle="bold" />
<SeekBar
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:backgroundTint="@color/md_green_100"
android:id="@+id/transferSwitch"
android:elevation="5dp"
android:foregroundTint="@color/md_green_100"
android:indeterminateTint="@color/md_green_100"
android:max="2"
android:padding="5dp"
android:progress="1"
android:progressBackgroundTint="@color/md_green_100"
android:progressTint="@color/md_green_100"
android:secondaryProgressTint="@color/md_green_100"
android:thumb="@android:drawable/presence_online"
android:thumbTint="@color/md_green_800"
app:tickMarkTint="@color/md_green_800" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|end"
android:gravity="center"
android:padding="10dp"
android:text="@string/receive"
android:textColor="@color/colorPrimaryDark"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>用法:
SeekBar seekBar = findViewById(SEEKBAR);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//Do something here!
switch(progress){
case 0: //Left
case 2: //Right
case 1: default: //Center
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});预览:

https://stackoverflow.com/questions/9770230
复制相似问题