2

我已经设置了 seek bar 的属性。它到达一个进程,但它的进程没有到达 1,如下图所示

在此处输入图像描述

 sbpassangers.setProgress(1); //set this at onCreate

xml:

<SeekBar
      android:id="@+id/sBpassangers"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_toLeftOf="@+id/tvPassengers"
      android:layout_toRightOf="@+id/ivPhone"
      android:layout_weight="2"
      android:paddingBottom="3dp"
      android:paddingLeft="2dp"
      android:paddingTop="3dp" />

爪哇代码:

sbpassangers.setProgress(1);
    sbpassangers.setMax(sb1.getProgress());
    tvtaxipassangers.setText(Integer.toString(1));
    //sb1.setMax(9);
    //sb1.setProgress(2); // Set it to zero so it will start at the left-most edge
    sbpassangers.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            seekBar.setProgress(progress);
         if(progress == 0){
             seekBar.setProgress(1);
         }
            tvtaxipassangers.setText(Integer.toString(seekBar.getProgress()));

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {}

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {}

    });

应该是这样的:

在此处输入图像描述

请帮助解决这个问题

4

3 回答 3

7

交换两条线

    sbpassangers.setProgress(1);
    sbpassangers.setMax(sb1.getProgress());

sbpassangers.setMax(sb1.getProgress());
sbpassangers.setProgress(1);

除非您定义进度可以采用的最大值,否则您无法设置搜索栏的进度。

于 2013-10-29T10:57:49.473 回答
1

seekBar 是我们以自定义方式在应用程序中使用的重要组件。

请制作一个演示项目并按照以下代码到您的MainActivity

SeekbarActivity.java

package com.example.seekbarwidget;


import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class SeekbarActivity extends Activity { 
 TextView textview; 
 SeekBar seekbar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seekbar); 

        textview = (TextView)findViewById(R.id.textView);
        seekbar = (SeekBar)findViewById(R.id.seekBar1);
        seekbar.setMax(10);
        seekbar.setProgress(5);
        //initControls();

        seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { 
           //add here your implementation 
            } 
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { 
              //add here your implementation
            } 
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) { 

             textview.setText(" value = " +Integer.toString(progress));
            }
          });
    }

}


the activity_seekbar.xml is having following layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="125dp"
        android:indeterminate="false"
        android:max="10"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:progress="5"
        android:progressDrawable="@drawable/styled_progress"
        android:secondaryProgress="5"
        android:thumb="@drawable/thumbler_small" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Show seekbar value"
        android:textColor="#CD2134"
        android:textSize="27px"
        android:textStyle="bold" />

</LinearLayout>

finally the  styled_progress.xml inside drawable folder is having the lines

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+android:id/SecondaryProgress"
        android:drawable="@drawable/progress_cyan"/>
    <item
        android:id="@+android:id/progress"
        android:drawable="@drawable/progress_red"/>

</layer-list>

自定义搜索栏

于 2013-10-29T10:56:11.217 回答
0

要获得确切答案,您必须提供更多信息。大胆猜测一下,我会说您还需要设置 Max 值,

setMax(int)

更多信息请访问http://developer.android.com/reference/android/widget/ProgressBar.html

于 2013-10-29T10:57:08.263 回答