如何在android中为每个搜索条区间设置文本?

时间:2011-11-17 10:05:28

标签: android xml seekbar

我在android中创建了一个搜索栏,其最大值为12。

    <SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/mapview"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="32dp"
    android:max="12"
    android:progress="0"
    android:layout_marginTop="60dp"
    android:secondaryProgress="0" />

我必须在搜索栏下面的xml设计中设置1,2,3,4 .... 12这样的文本,对应于确切的间隔?怎么做到这一点?

3 个答案:

答案 0 :(得分:2)

首先,您会获得对SeekBar

的引用
SeekBar bar = (SeekBar) findViewById( R.id.seekBar1 );
bar.setOnSeekBarChangedListener( new OnSeek() );

接下来创建一个实现OnSeekBarChangedListener的私有类:

  private class OnSeek implements OnSeekBarChangeListener {
    public void onProgressChanged( SeekBar seekBar, int progress,
                                   boolean fromUser ) {
      //Here is where you handle the change in the seekbar e.g.
      myTextView.setText( "You are at stage: " + progress );    
    }

    public void onStartTrackingTouch( SeekBar seekBar ) {
      //No need to do stuff here
    }

    public void onStopTrackingTouch( SeekBar seekBar ) {
      //No need to do stuff here
    }
  }

答案 1 :(得分:1)

请使用以下代码

<seek bar object>.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
            {

                @Override
                public void onProgressChanged(SeekBar arg0, int arg1,
                        boolean arg2) {
                    // TODO Auto-generated method stub
                    int mCurrentValue = arg1 + mMinValue;
                    text.setText(Integer.toString(mCurrentValue));
                }

                @Override
                public void onStartTrackingTouch(SeekBar arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onStopTrackingTouch(SeekBar arg0) {
                    // TODO Auto-generated method stub

                }

            });

答案 2 :(得分:0)

package com.example.test_project;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.SeekBar;

public class CustomSeekBar extends SeekBar{
Paint paint;
int heigth;
int width;
int interval;
int Startpoint;
int StartingPoint;
int EndingPoint;
int Differnce;
private boolean StartUp;
    public CustomSeekBar(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        paint=new Paint();
        paint.setColor(Color.BLACK);
        paint.setTextSize(30.0f);
        StartUp=false;

    }
    public void IntializeValue(int startingPoint,int endingPoint,int difference)
    {
        this.StartingPoint=startingPoint;
        this.EndingPoint=endingPoint;
this.Differnce=difference;
setProgress(2);
setMax(12);
    }

    @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            heigth=h;
            width=w;
            int parts=(EndingPoint/Differnce);
            interval=width/parts;
            System.out.println("111 width is "+width);
            System.out.println("111 heigth is "+heigth);
            System.out.println("111 interval is "+interval);
            super.onSizeChanged(w, h, oldw, oldh);
        }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        int diff=10;
            System.out.println("ondraw 2");
            int tempPoint=StartingPoint;
            canvas.drawLine(Startpoint,15,width,15,paint);
            canvas.drawLine(Startpoint,5,Startpoint,25,paint);
        canvas.drawText(tempPoint+"", Startpoint,heigth, paint);
        canvas.drawLine(Startpoint,5,Startpoint,25,paint);
        Startpoint+=interval;
        tempPoint+=Differnce;
        canvas.drawText(tempPoint+"", Startpoint-diff,heigth, paint);
        canvas.drawLine(Startpoint,5,Startpoint,25,paint);
        Startpoint+=interval;
        tempPoint+=Differnce;
        canvas.drawText(tempPoint+"", Startpoint-diff,heigth, paint);
        canvas.drawLine(Startpoint,5,Startpoint,25,paint);
        Startpoint+=interval;
        tempPoint+=Differnce;
        canvas.drawText(tempPoint+"", Startpoint-diff,heigth, paint);
        canvas.drawLine(Startpoint,5,Startpoint,25,paint);
        Startpoint+=interval;
        tempPoint+=Differnce;
        canvas.drawText(tempPoint+"", Startpoint-diff,heigth, paint);
        canvas.drawLine(Startpoint,5,Startpoint,25,paint);
        Startpoint+=interval;
        tempPoint+=Differnce;
        canvas.drawText(tempPoint+"", Startpoint-diff,heigth, paint);
        canvas.drawLine(Startpoint,5,Startpoint,25,paint);
        Startpoint+=interval;
        tempPoint+=Differnce;
        diff=10;
        canvas.drawText(tempPoint+"", Startpoint-diff,heigth, paint);
        canvas.drawLine(Startpoint,5,Startpoint,25,paint);
        Startpoint+=interval;
        tempPoint+=Differnce;
        canvas.drawText(tempPoint+"", Startpoint-diff,heigth, paint);
        canvas.drawLine(Startpoint,5,Startpoint,25,paint);
        Startpoint+=interval;
        tempPoint+=Differnce;
        canvas.drawText(tempPoint+"", Startpoint-diff,heigth, paint);
        canvas.drawLine(Startpoint,5,Startpoint,25,paint);
        Startpoint+=interval;
        tempPoint+=Differnce;
        canvas.drawText(tempPoint+"", Startpoint-diff,heigth, paint);
        canvas.drawLine(Startpoint,5,Startpoint,25,paint);
        Startpoint+=interval;

        super.onDraw(canvas);
        Startpoint=0;
        System.out.println("ondraw");
    }

}