Android:如何设置SeekBar的值

时间:2015-01-14 06:44:38

标签: android android-fragments android-seekbar

我想设置SeekBar值,但问题是我将SeekBarTextView视图设置为Slidemenu中的inflater方法,而不是任何onCreate方法,因此我无法使用findViewById引用SeekBar。请告诉我哪里出错了。

这是布局fragment_budget_filter.xml文件:

 <TextView

     android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Rs. 5L"
    android:id="@+id/max_budget"
/>

<SeekBar

 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:id="@+id/budget_max_seekbar"
  />

我的Java文件如下所示:

 public class BudgetFragment extends Fragment { 
public BudgetFragment(){}

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

    View rootView = inflater.inflate(R.layout.fragment_budget_filter, container, false);
    return rootView;
 }  
}

如何在上面的java文件中调用以下方法?

 public class AndroidSeekBar{

// declare text objects variables
private SeekBar PRICEbar; 
private TextView SeekValue;
private TextView PRICEtextProgress,DISTANCEtextProgress, RATINGtextProgress;

public AndroidSeekBar(SeekBar id1, TextView textid){

    PRICEbar = id1; // make seekbar object
    SeekValue = textid;
    int progress = PRICEbar.getProgress();
   //        PRICEbar.setOnSeekBarChangeListener(this); 
    PRICEbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){ 

           @Override 
           public void onProgressChanged(SeekBar seekBar, int progress, 
             boolean fromUser) { 
            // TODO Auto-generated method stub 
            SeekValue.setText(String.valueOf(progress)); 
           } 

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

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

1 个答案:

答案 0 :(得分:0)

  

如何在上面的java文件中调用以下方法?

onCreateView(LayoutInflater, ViewGroup, Bundle)中,您仍然可以使用findViewById(int)。以下将调用您想要的构造函数:

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

...

SeekBar seekBar = (SeekBar) rootView.findViewById(R.id.budget_max_seekbar);
TextView textView = (TextView) rootView.findViewById(R.id.max_budget);
AndroidSeekBar yourSeekBar = new AndroidSeekBar(seekBar, textView);

...

return rootView;