Android的多状态切换

问题描述:

我想创造这样的滑块: enter image description hereAndroid的多状态切换

有没有在android系统的方式来定义一个三个态滑盖的?我对android开发很陌生,所以请尽可能多地包含示例代码,这将非常有帮助。

+0

你有能还解决这个问题?如果是,请发布您的答案! – Milan 2013-01-22 02:35:32

+0

我认为你需要的只是一个自定义的RatingBar:http://www.b2creativedesigns.com/ratingbar.php – 2014-05-19 17:48:29

一个简单的例子可以像下面的代码。你可以玩一点图形,你会得到你想要的。

布局/ main.xml中

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/TableLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="10dp" > 

    <include 
     android:id="@+id/tableRow1_ref" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     layout="@layout/description" /> 

    <SeekBar 
     android:id="@+id/seekBar1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="10dp" 
     android:max="100" /> 

    <include 
     android:id="@+id/tableRow1_ref" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     layout="@layout/description" /> 

    <SeekBar 
     android:id="@+id/seekBar2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:max="100" /> 

</TableLayout> 

布局/ description.xml

<?xml version="1.0" encoding="utf-8"?> 
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/tableRow1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="left" 
     android:paddingLeft="5dp" 
     android:text="@string/kid" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:text="@string/adult" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="right" 
     android:paddingRight="5dp" 
     android:text="@string/senior" /> 

</TableRow> 

MainActivity.java

package com.test.Slider; 

import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.widget.SeekBar; 
import android.widget.SeekBar.OnSeekBarChangeListener; 

public class MainActivity extends ActionBarActivity 
     implements OnSeekBarChangeListener{ 

    SeekBar sb1, sb2; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     sb1 = (SeekBar)findViewById(R.id.seekBar1); 
     sb2 = (SeekBar)findViewById(R.id.seekBar2); 

     sb1.setOnSeekBarChangeListener(this); 
     sb2.setOnSeekBarChangeListener(this); 
    } 

    @Override 
    public void onProgressChanged(SeekBar seekBar, int progress, 
      boolean fromUser) { 
     // we don't need it 
    } 

    @Override 
    public void onStartTrackingTouch(SeekBar seekBar) { 
     // we don't need it  
    } 

    @Override 
    public void onStopTrackingTouch(SeekBar seekBar) { 
     int mProgress = seekBar.getProgress(); 
     if(mProgress > 0 & mProgress < 26) { 
      seekBar.setProgress(0); 
     } else if(mProgress > 25 & mProgress < 76) { 
      seekBar.setProgress(50); 
     } else seekBar.setProgress(100); 
    } 
} 

输出:

screenshot of sliders

有这个好的库:android-comboseekbar。 使用它或看看它是如何工作的。

屏幕 Screen

+0

虽然这个链接可能回答这个问题,但最好在这里包含答案的基本部分并提供链接参考。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/ review/low-quality-posts/17377976) – 2017-09-19 11:21:21