14

过滤器.java

public class Filters extends Activity implements OnSeekBarChangeListener{
    private SeekBar PRICEbar,DISTANCEbar, RATINGbar; // declare seekbar object variable
    // declare text label objects
    private TextView PRICEtextProgress,DISTANCEtextProgress, RATINGtextProgress;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // load the layout
        setContentView(R.layout.filters);     
        PRICEbar = (SeekBar)findViewById(R.id.PRICEseekBarID); // make seekbar object
        PRICEbar.setOnSeekBarChangeListener(this); 
        PRICEbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

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

            }

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

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub
                PRICEtextProgress = (TextView)findViewById(R.id.PRICEtextViewProgressID);
                PRICEtextProgress.setText("Price:: Rs "+progress);

            }
        });

}
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (seekBar == PRICEbar)
            PRICEtextProgress.setText("Price:: Rs "+progress);
    }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

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

    }



}

过滤器.xml

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

    <RelativeLayout
        android:id="@+id/relativeLayoutforPRICE"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/PRICEtextViewProgressID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Price"
            android:textAppearance="?android:attr/textAppearanceLarge" >
        </TextView>

        <SeekBar
            android:id="@+id/PRICEseekBarID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="26dp"            
            android:max="100" >
        </SeekBar>
    </RelativeLayout>
</LinearLayout>

我有这样的输出::

用于选择数量的令人讨厌的滑块的图像

如何修改我的代码以在搜索栏中获取初始值和最终值,如下图所示?

令人讨厌的滑块,最小值为 0,最大值为 100,但这些值应该可以从其他输入配置

4

5 回答 5

9

您已经定义了 seekbar 的最大值

android:max="100"

现在您需要在 seekbar 的左侧和右侧添加两个文本视图。使用相对布局并将两个文本视图左右对齐到父级。

于 2013-10-06T04:57:30.043 回答
6

Brontok的提示解决了我的问题。我只需要更改 XML。我想在这里分享结果:

<RelativeLayout
        android:id="@+id/relativeLayoutforPRICE"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/PRICEtextViewProgressID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Price"
            android:textAppearance="?android:attr/textAppearanceLarge" >
        </TextView>

        <SeekBar
            android:id="@+id/PRICEseekBarID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="26dp"
            android:max="100" >
        </SeekBar>

        <TextView
            android:id="@+id/PRICEinitialvaluetextID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/PRICEseekBarID"
            android:text="Rs 0" >
        </TextView>

        <TextView
            android:id="@+id/PRICEinitialvaluetextID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/PRICEseekBarID"
            android:text="Rs 100" />
    </RelativeLayout>
于 2013-10-06T05:26:25.573 回答
2

首先,我认为你应该扩展SeekBar.
其次,调用canvas.drawText()inonDraw()方法在之前SeekBar和之后绘制文本SeekBar

这样你就可以textView在你的onDraw方法回调中刷新

于 2013-10-06T04:59:41.387 回答
1

检查出来,你实现了 OnSeekBarChangeListener 创建的方法,并设置

public class Filters extends Activity implements OnSeekBarChangeListener{

 PRICEbar.setOnSeekBarChangeListener(this); 

下一行你用新的 OnSeekBarChangeListener 覆盖它

 PRICEbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

您可以从您的活动中删除实现 OnSeekBarChangeListener 和这 3 个空方法(onSeekBarChange、onStartTracking、onStopTracking)

于 2015-06-16T14:38:28.030 回答
1

只是为了好玩,在 main.xml 中使用空的线性布局,带有 seekbar 的示例

import android.app.*;
import android.os.*;
import android.view.*;
import android.content.*;
import android.graphics.*;
import android.graphics.Color.*;
import android.graphics.drawable.*;
import android.graphics.drawable.shapes.*;
import android.widget.*;
import android.widget.ProgressBar;
import android.text.*;
//import org.apache.http.impl.conn.*;
import android.util.*;
import android.view.View.*;
import android.widget.SeekBar.*;

public class MainActivity extends Activity {
    public int angle=30;
    @Override
    //CustomDrawableView customDrawableView;
    LinearLayout myLayout;

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

        myLayout = findViewById(R.id.linear_layout);

        //setContentView(customDrawableView);

/*      TextView text1 = new TextView(this);
        text1.setText(R.string.hello_world);
        myLayout.addView(text1);
    */  
        final TextView text2 = new TextView(this);
        text2.setText("30");
        myLayout.addView(text2);

        SeekBar seek = new SeekBar(this);
        //seek.setMin(2);
        seek.setMax(180);
        seek.setProgress(30);
        myLayout.addView(seek);

        final CustomDrawableView customDrawableView = new CustomDrawableView(this);
        myLayout.addView(customDrawableView);
        customDrawableView.setBackgroundColor(0x00888888);

        seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
                public void onProgressChanged(SeekBar seek,int value,boolean usr){
                    angle = value;
                    if (angle<=2) angle = 2;
                    text2.setText(String.valueOf(value));
                    customDrawableView.invalidate();
                }
                public void onStartTrackingTouch(SeekBar seek){

                }
                public void onStopTrackingTouch(SeekBar seek){

                }
            });

        //customDrawableView.setRotationX(45);

    }; // MainActivity_onCreate

    public class CustomDrawableView extends View {

        private int n=16;
        private ShapeDrawable[] drws = new ShapeDrawable[4*n];
        protected Canvas canvas;

        public CustomDrawableView(Context context) {
            super(context);

            setContentDescription(context.getResources().getString(
                                      R.string.my_view_desc));
            angle = 30;
        };

        protected void onDraw(Canvas oCanvas) {
            canvas = oCanvas;
            show();

        }; // onDraw

        public void show() {

            int cx = canvas.getWidth()/2;
            int cy = canvas.getHeight()/2;
            int color = Color.argb(0x88,0x00,0x99,0xff);
            int stp;

            for (int s=0;s<180;s+=angle){
                canvas.rotate(angle,cx,cy);
                for (int i=0;i<=15;i++){

                    stp = 29 - i;

                    color = Color.argb(i*10, i*17, 0xaa, 0xff - i*10);

                    drws[i] = new ShapeDrawable(new OvalShape());
                    drws[i].getPaint().setColor(color);
                    drws[i].setBounds(cx+stp*i, cy+10, cx+30+stp*i, cy+20);

                    drws[n+i] = new ShapeDrawable(new OvalShape());
                    drws[n+i].getPaint().setColor(color);
                    drws[n+i].setBounds(cx+10+stp*i, cy, cx+20+stp*i, cy+30);

                    drws[2*n+i] = new ShapeDrawable(new OvalShape());
                    drws[2*n+i].getPaint().setColor(color);
                    drws[2*n+i].setBounds(cx-stp*i-30, cy-20, cx-stp*i, cy-10);

                    drws[3*n+i] = new ShapeDrawable(new OvalShape());
                    drws[3*n+i].getPaint().setColor(color);
                    drws[3*n+i].setBounds(cx-stp*i-20, cy-30, cx-stp*i-10, cy);

                    canvas.rotate(22.5f,cx,cy);
                    drws[i].draw(canvas);
                    drws[n+i].draw(canvas);
                    drws[2*n+i].draw(canvas);
                    drws[3*n+i].draw(canvas);

                }
                canvas.rotate(angle,cx,cy);
            }
        }
    }; // CustomDrawableView

};  // MainActivity
于 2020-06-20T01:55:40.877 回答