首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Android中实现三向滑动按钮

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

Stack Overflow用户
提问于 2012-03-19 20:30:15
回答 4查看 3.3K关注 0票数 7

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

我如何实现这一点?

EN

Stack Overflow用户

发布于 2018-10-16 01:43:08

给你,我根据自己的需要开发了它,我正在分享代码。非常容易做和配置。只需使用switch语句查找指针位置并执行某些操作即可。

代码语言:javascript
复制
<?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>

用法:

代码语言:javascript
复制
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) {

    }
});

预览:

票数 0
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9770230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档