如何为RangeSeekbar使用onProgressChanged方法?

时间:2014-04-04 18:20:43

标签: android range seekbar

我已成功将此网站(http://code.google.com/p/range-seek-bar/)的RangeSeekbar实施到我的应用中。

现在很多人都有问题,就是我们找不到RangeSeekbar代码中两个拇指的onProgressChanged方法。

我是否必须自己编写onProgressChanged?

我将RangeSeekbar实现为DialogFragment。对于普通的Seekbar来说一切正常。

http://www.directupload.net/file/d/3582/mokh6mkb_jpg.htm< - 如何在“to”的左侧和右侧显示两个TextView,以显示RangeSeekbar的选定值?

我的代码到目前为止:

package com.example.test2;

import com.example.test2.RangeSeekBar.OnRangeSeekBarChangeListener;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.TextView;

public class LookingAgeFragment extends DialogFragment implements View.OnClickListener, OnRangeSeekBarChangeListener {

    protected static final String TAG = null;
    public static String username; 
    static int lowestAge;
    static int highestAge;
    static TextView viewTo, viewMaxAge, viewMinAge;
    RangeSeekBar<Integer> seekBar;


    public void onAttach(Activity activity){
        super.onAttach(activity);
    }


    public void onCeate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);



    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view= inflater.inflate(R.layout.fragment_age_looking,null);
        getDialog().setTitle(getString(R.string.lookingage_title));
        Button button = (Button)view.findViewById(R.id.buttonLeaveFragmentLookingAge);
        viewTo = (TextView)view.findViewById(R.id.textView_to);
        viewMaxAge = (TextView)view.findViewById(R.id.textView_maxAge);
        viewMinAge = (TextView)view.findViewById(R.id.textView_minAge);
        button.setOnClickListener(this);

                // create RangeSeekBar as Integer range between 20 and 75
        seekBar = new RangeSeekBar<Integer>(20, 75, getActivity());
        seekBar.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>() {
                @Override
                public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
                        // handle changed range values
                        Log.i(TAG, "User selected new range values: MIN=" + minValue + ", MAX=" + maxValue);
                }
        });

        ViewGroup layout = (ViewGroup) view.findViewById(R.id.rangeSeekbarContainer);
        layout.addView(seekBar);

        setUpLayout();
        return view;


    }

    @SuppressLint("NewApi")
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        Bundle bundle = this.getArguments();
        if(bundle != null){
        username = bundle.getString("username", null);
        }
    }


    private void setUpLayout() {
        // TODO Auto-generated method stub

        setCancelable(false);

        //bar.setOnSeekBarChangeListener(yourSeekBarListener);

    }


    public void saveDataChange(String s, String data){
        String help = "com.example.test2.PREFERENCE_FILE_KEY_"+username;
        Context context = getActivity();
        SharedPreferences sharedPref = context.getSharedPreferences(help, Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(s, data);
        editor.commit();
    }

    public String getData(String s){
        String data;
        String help = "com.example.test2.PREFERENCE_FILE_KEY_"+username; 
        Context context = getActivity();
        SharedPreferences sharedPref = context.getSharedPreferences(help, Context.MODE_PRIVATE);
        data = sharedPref.getString(s, null);
        return data;
    }


    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub
        switch(view.getId()){
            case R.id.buttonLeaveFragmentLookingAge:
            dismiss();  

            break;

        }
    }

    public void onProgressChanged() {
        // TODO Auto-generated method stub
        int maxvalue = (Integer) seekBar.getSelectedMaxValue();
        viewMaxAge.setText(maxvalue);
        int minvalue = (Integer) seekBar.getSelectedMinValue();
        viewMinAge.setText(minvalue);
    }

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

    }

    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
        int maxvalue = (Integer) seekBar.getSelectedMaxValue();
        viewMaxAge.setText(maxvalue);
        int minvalue = (Integer) seekBar.getSelectedMinValue();
        viewMinAge.setText(minvalue);
    }

public void onRangeSeekBarValuesChanged(RangeSeekBar bar, Object minValue,
            Object maxValue) {
        // TODO Auto-generated method stub
        viewMaxAge.setText((String)maxValue);

        viewMinAge.setText((String)minValue);
    }


}

感谢您的帮助。

3 个答案:

答案 0 :(得分:3)

RangeSeekbar是一个通用类。如果您将T参数传递给它作为范围值的类型,则RangSeekBar将左拇指值传递为minValue,将右拇指值传递为maxValue in RangeSeekBar.OnRangeSeekBarChangeListener。(RangeSeekBar bar,T minValue,T maxValue)。

你班级的改变行延伸到:

public class LookingAgeFragment extends DialogFragment implements OnRangeSeekBarChangeListener<Integer>

之后,您应该在此行的代码中将Object更改为Integer

    seekBar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() {
        @Override
        public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
            viewMinAge.setText(String.valueOf(minValue));
            viewMaxAge.setText(String.valueOf(minValue));
        }
    });

我认为通过这种改变,你的问题将得到解决。 对于更通用的解决方案,当您的年龄范围不连续且可能是离散的时,我建议使用此解决方案

public class LookingAgeFragment extends DialogFragment implements RangeSeekBar.OnRangeSeekBarChangeListener<Integer> {

RangeSeekBar<Integer> seekBar;
int[] ageValues={10, 20, 30, 50, 100};


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    // create RangeSeekBar as Integer range between 20 and 75
    seekBar = new RangeSeekBar<Integer>(0, ageValues.length - 1, getActivity());
    seekBar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() {
        @Override
        public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
            viewMinAge.setText(String.valueOf(ageValues[minValue]));
            viewMaxAge.setText(String.valueOf(ageValues[minValue]));
        }
    });
}

}

答案 1 :(得分:1)

我使用下一代码来获取更改事件的值:

rangeSeekBar.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d("MinValue", rangeSeekBar.getSelectedMinValue().toString());
                Log.d("MaxValue", rangeSeekBar.getSelectedMaxValue().toString());
                return false;
            }
        });

答案 2 :(得分:0)

在ReangeSekkBar课程中,如果在ACTION_MOVE

中,你可以使用它
 if (notifyWhileDragging && listener != null) {
 listener.onRangeSeekBarValuesChanged(this,   getSelectedMinValue(), getSelectedMaxValue());
   }

如果

,请删除此项
//if (notifyWhileDragging && listener != null) {
 listener.onRangeSeekBarValuesChanged(this,   getSelectedMinValue(), getSelectedMaxValue());
   //}