我已经成功地将rangeseekbar从这个网站(http://code.google.com/p/range-seek-bar/)实现到我的应用程序中。
现在很多人的问题是,我们在rangeseekbar的代码中找不到两个拇指的onprogresschanged方法。
我必须写我自己的进步改变吗?
我将rangeseekbar实现为一个对话框片段。正常情况下一切正常。
http://www.directupload.net/file/d/3582/mokh6mkb_jpg.htm我的代码:
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);
}
}
谢谢你的帮助。
最佳答案
rangeseekbar是一个泛型类。如果将t参数作为范围值的类型传递给它,则rangseekbar在
rangeseekbar.onrangeseekbarchargelistener。(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]));
}
});
}
}