Filters.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); // set seekbar listener.
// since we are using this class as the listener the class is "this"
// make text label for progress value
PRICEtextProgress = (TextView)findViewById(R.id.PRICEtextViewProgressID);
// make text label for action
//textAction = (TextView)findViewById(R.id.textViewAction);
////////////////////////////////////////////////////////
DISTANCEbar = (SeekBar)findViewById(R.id.DISTANCEseekBarID); // make seekbar object
DISTANCEbar.setOnSeekBarChangeListener(this);
DISTANCEtextProgress = (TextView)findViewById(R.id.DISTANCEtextViewProgressID);
////////////////////////////////////////////////////////
RATINGbar = (SeekBar)findViewById(R.id.RATINGseekBarID); // make seekbar object
RATINGbar.setOnSeekBarChangeListener(this);
RATINGtextProgress = (TextView)findViewById(R.id.RATINGtextViewProgressID);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
// change progress text label with current seekbar value
PRICEtextProgress.setText("Price:: Rs "+progress);
// change action text label to changing
//textAction.setText("changing");
DISTANCEtextProgress.setText("Distance:: "+progress);
RATINGtextProgress.setText("Rating:: "+progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
//textAction.setText("starting to track touch");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
seekBar.setSecondaryProgress(seekBar.getProgress());
//textAction.setText("ended tracking touch");
}
}
filters.xml
<?xml version="1.0" encoding="utf-8"?>
<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>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
<RelativeLayout
android:id="@+id/relativeLayoutforDISTANCE"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/DISTANCEtextViewProgressID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="DISTANCE"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
<SeekBar
android:id="@+id/DISTANCEseekBarID"
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>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
<RelativeLayout
android:id="@+id/relativeLayoutforRATING"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/RATINGtextViewProgressID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Rating"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
<SeekBar
android:id="@+id/RATINGseekBarID"
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>
我们可以清楚地知道无论哪个搜索栏都改变了所有的价值 立刻改变
我该如何解决这个问题?
另外,如何让评分搜索栏接受( 1,2,3,4,5 )
之类的值
而不是(1234..100)
有什么想法吗?
答案 0 :(得分:1)
当值改变时,所有SeekBars都在同一个对象(this)上调用相同的方法。你可以做两件事:
在您的处理程序方法中,检查哪个SeekBar触发了调用。
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (seekBar == PRICEbar)
PRICEtextProgress.setText("Price:: Rs "+progress);
else if (seekBar == DISTANCEbar)
DISTANCEtextProgress.setText("Distance:: "+progress);
else if (seekBar == RATINGbar)
RATINGtextProgress.setText("Rating:: "+progress);
}
您也可以使用内联匿名类
public class Filters extends Activity { //Remove implements OnSeekBarChangeListener
...
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// load the layout
setContentView(R.layout.filters);
// make text label for progress value
PRICEtextProgress = (TextView)findViewById(R.id.PRICEtextViewProgressID);
PRICEbar = (SeekBar)findViewById(R.id.PRICEseekBarID); // make seekbar object
PRICEbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
PRICEtextProgress.setText("Price:: Rs "+progress);
}
});
////////////////////////////////////////////////////////
DISTANCEtextProgress = (TextView)findViewById(R.id.DISTANCEtextViewProgressID);
DISTANCEbar = (SeekBar)findViewById(R.id.DISTANCEseekBarID); // make seekbar object
DISTANCEbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
DISTANCEtextProgress.setText("Distance:: "+progress);
}
});
////////////////////////////////////////////////////////
RATINGtextProgress = (TextView)findViewById(R.id.RATINGtextViewProgressID);
RATINGbar = (SeekBar)findViewById(R.id.RATINGseekBarID); // make seekbar object
RATINGbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener {
@Override
public void onProgressChanged(
SeekBar seekBar, int progress, boolean fromUser) {
RATINGtextProgress.setText("Rating:: "+progress);
}
});
}
最后, 您可以使用seekBar.setMax(int)将搜索栏的最大值限制为5。