如何在Android中实现三向滑动按钮

时间:2012-03-19 12:30:15

标签: android button

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

enter image description here

enter image description here

enter image description here

我该如何实现?

4 个答案:

答案 0 :(得分:6)

这实际上就像是搜索栏的延伸。

要实现这种滑块,我会创建一个扩展搜索栏的类。 在构造函数中,我将使用 .setMax(2); ,这意味着搜索栏只有3个位置/步骤。

然后在默认实现中,我将使用 seekbar.OnChangeListener()。在 pogressChanged(...)方法中,使用 .setBackgroundDrawable(...)切换背景图像,并在必要时使用 .setThumb( ...)到你想要的形象。

如果您甚至想要更改滑块的位置,可以实现Click事件处理,并使用 .setProgress();

更改滑块的位置

这是一个非常容易处理和快速实现的实现,因为它只需要几行代码。

答案 1 :(得分:1)

这可以使用RadioGroup实现。

您必须自定义RadioGroup中的RadioButtons,如下所示:

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/background_toggler"
    android:orientation="horizontal"
    android:padding="4dp">

    <RadioButton
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button1_background"
        android:button="@null"
        android:checked="true" />

    <RadioButton
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button2_background"
        android:button="@null"
        android:checked="false" />

    <RadioButton
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button3_background"
        android:button="@null"
        android:checked="false" />

</RadioGroup>

抽拉/ button1_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/btn1checked"
        android:state_checked="true" />
    <item
        android:drawable="@drawable/btn1unchecked"
        android:state_checked="false" />
</selector>

抽拉/ button2_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/btn2checked"
        android:state_checked="true" />
    <item
        android:drawable="@drawable/btn2unchecked"
        android:state_checked="false" />
</selector>

抽拉/ button3_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/btn3checked"
        android:state_checked="true" />
    <item
        android:drawable="@drawable/btn3unchecked"
        android:state_checked="false" />
</selector>

为所有3个按钮使用相关的可绘制资源。 现在,您可以处理RadioGroup检查更改的事件。

答案 2 :(得分:0)

创建一个新视图并覆盖onPaint()方法。

在位置A,B或C(最左侧,中间,最右侧)绘制“滑块”位图,然后设置视图当前所在的A,B,C位置的背景颜色。使用Canvas。 drawBitmap() - http://developer.android.com/reference/android/graphics/Canvas.html#drawBitmap(android.graphics.Bitmap,float,float,android.graphics.Paint)绘制位图

您可以使用 - Canvas.drawRGB()绘制背景颜色之前绘制滑块位图

进行某种事件处理以更改位置,例如实现onClickListener并监听点击。当你得到一个点击时,只需在该位置加1,所以A - &gt; B,B - &gt; C,C - &gt; A等等。

答案 3 :(得分:0)

在这里,我根据自己的需要开发了它,正在共享代码。相当容易做到和配置。只需使用 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) {

    }
});

预览:

Sweet 3-way switch