如何在Fragment中使用SeekBar值?

时间:2014-11-10 12:02:53

标签: android android-fragments android-seekbar

  

我正在构建和Android应用程序,我正在使用seekbar   最大值和最小值。

     

我用过这个链接的例子 -   http://www.kpbird.com/2011/05/android-seek-bar-with-two-thumb.html

     

但是当我在片段Activity中使用它时,我无法获得搜索栏值。   这是我编辑的代码 -

package tabsswipe;

public class FragmentPlay extends Fragment {

String selItem,selItem2,selItem3,selItem4;
TextView tv1,tv2;
SeekBarWithTwoThumb swtt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_play, container, false);

     getActivity().getActionBar().setBackgroundDrawable(
                new ColorDrawable(Color.parseColor("#0077d1")));
        ActionBar mActionBar = getActivity().getActionBar();
        getActivity().getActionBar().setIcon(
           new ColorDrawable(getResources().getColor(android.R.color.transparent)));    
        mActionBar.setDisplayShowHomeEnabled(true);
        mActionBar.setDisplayShowTitleEnabled(false);
        LayoutInflater mInflater = LayoutInflater.from(getActivity());
        View mCustomView = mInflater.inflate(R.layout.custom_actionbar2, null);
        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);

        ImageButton btn = (ImageButton) getActivity().findViewById(R.id.button);
        btnt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                viewCategory();

            }
        });


    return rootView;
    }

    private void viewCategory() {

        AlertDialog.Builder viewDialog = new AlertDialog.Builder(getActivity());

        viewDialog.setTitle("selecte");

        LayoutInflater li = (LayoutInflater) 
   getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View dialogView = li.inflate(R.layout.customealertdialogbox, null);
        viewDialog.setView(dialogView);

        viewDialog.setPositiveButton("Ok",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        new DownloadJSON().execute();   
                    }
                });

        viewDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                    }
                });

        swtt = (SeekBarWithTwoThumb) dialogView.findViewById(R.id.myseekbar);
        swtt.setSeekBarChangeListener(getActivity());

        //Button btn = (Button) dialogView.findViewById(R.id.button1);
        //btn.setOnClickListener(new OnClickListener() {

            //@Override
            //public void onClick(View v) {
                // TODO Auto-generated method stub
                //Toast.makeText(getActivity(), "this is my Toast message!!! =)",
                        //   Toast.LENGTH_LONG).show();
            //}
    //  });
        viewDialog.show();




    }
    public class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH); 
    Toast.makeText(getActivity(), year,
               Toast.LENGTH_LONG).show();
    // Create a new instance of DatePickerDialog and return it

    return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
    Calendar c = Calendar.getInstance();
    c.set(year, month, day);

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(c.getTime());
    Toast.makeText(getActivity(), year,
               Toast.LENGTH_LONG).show();
    }

}

}

这是搜索栏库 -

package libseekbar;

public class SeekBarWithTwoThumb extends ImageView {

private String TAG = this.getClass().getSimpleName();
private Bitmap thumb = BitmapFactory.decodeResource(getResources(),R.drawable.leftthumb);
private int thumb1X, thumb2X;
private int thumb1Value, thumb2Value;
private int thumbY;
private Paint paint = new Paint();
private int selectedThumb;
private int thumbHalfWidth;
private SeekBarChangeListener scl;


public SeekBarWithTwoThumb(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public SeekBarWithTwoThumb(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SeekBarWithTwoThumb(Context context) {
    super(context);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    if (getHeight() > 0)
        init();
}

private void init() {
    printLog("View Height =" + getHeight() + "\t\t Thumb Height :"+ thumb.getHeight());
    if (thumb.getHeight() > getHeight())
        getLayoutParams().height = thumb.getHeight();

    thumbY = (getHeight() / 2) - (thumb.getHeight() / 2);
    printLog("View Height =" + getHeight() + "\t\t Thumb Height :"+ thumb.getHeight() + "\t\t" + thumbY);

    thumbHalfWidth = thumb.getWidth()/2;
    thumb1X = thumbHalfWidth;
    thumb2X = getWidth()/2 ;
    invalidate();
}
public void setSeekBarChangeListener(SeekBarChangeListener scl){
    this.scl = scl;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(thumb, thumb1X - thumbHalfWidth, thumbY,paint);
    canvas.drawBitmap(thumb, thumb2X - thumbHalfWidth, thumbY,paint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int mx = (int) event.getX();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        if (mx >= thumb1X - thumbHalfWidth
                && mx <= thumb1X + thumbHalfWidth) {
            selectedThumb = 1;
            printLog("Select Thumb 1");
        } else if (mx >= thumb2X - thumbHalfWidth
                && mx <= thumb2X + thumbHalfWidth) {
            selectedThumb = 2;
            printLog("Select Thumb 2");
        }
        break;
    case MotionEvent.ACTION_MOVE:
        printLog("Mouse Move : " + selectedThumb);

        if (selectedThumb == 1) {
            thumb1X = mx;
            printLog("Move Thumb 1");
        } else if (selectedThumb == 2) {
            thumb2X = mx;
            printLog("Move Thumb 2");
        }
        break;
    case MotionEvent.ACTION_UP:
        selectedThumb = 0;
        break;
    }

    if(thumb1X < 0)
        thumb1X = 0;

    if(thumb2X < 0)
        thumb2X = 0;

    if(thumb1X > getWidth() )
        thumb1X =getWidth() ;

    if(thumb2X > getWidth() )
        thumb2X =getWidth() ;

    invalidate();
    if(scl !=null){
        calculateThumbValue();
        scl.SeekBarValueChanged(thumb1Value,thumb2Value);
    }
    return true;
}

private void calculateThumbValue(){
    thumb1Value = (100*(thumb1X))/(getWidth());
    thumb2Value = (100*(thumb2X))/(getWidth());
}
private void printLog(String log){
    Log.i(TAG, log);
}

interface SeekBarChangeListener{
    void SeekBarValueChanged(int Thumb1Value,int Thumb2Value);
}

public void setSeekBarChangeListener(FragmentActivity activity, SeekBarChangeListener scl) {
    // TODO Auto-generated method stub
    this.scl = scl;
}   
}

2 个答案:

答案 0 :(得分:0)

正如您通过阅读库的代码所看到的,它使用回调方法在滑块值发生更改时通知您(SeekBarValueChanged(int,int))。

所以你需要做的是在片段

中实现SeekBarChangeListener
public Class FragmentPlayo extends Fragment implements SeekBarChangeListener {

然后在Fragment中实现方法(回调),以便在值更改时执行您想要执行的操作:

private void SeekBarValueChanged(int thumb1Value, int thumb2Value) {
//Your code goes here when the values have changed
}

希望这有帮助!

编辑:您还必须添加

swtt = (SeekBarWithTwoThumb) dialogView.findViewById(R.id.myseekbar);
swtt.setSeekBarChangeListener(this);

因为它意味着&#34;嘿,告诉我,当价值观发生变化时,我会照顾他们!&#34;

答案 1 :(得分:0)

@Hitesh在对话框中使用此代码。

    tv1.setText("Thumb 1 Value :" +Thumb1Value);
    tv2.setText("Thumb 2 Value :"+Thumb2Value) ;