Giá trị tối thiểu phải là 0 đối với đối tượng SeekBar trong Java (nghĩa là, nó không thể thay đổi), nhưng đây là cách để có được giao diện và hiệu suất được yêu cầu.
Giả sử bạn muốn tối thiểu của bạn là 5 và tối đa của bạn là 100 ...
Vì vậy, bạn có một loạt các 95, vì vậy bạn sẽ cần phải thiết lập các SeekBar với một giá trị tối đa 95
.
Nếu thanh tìm kiếm của bạn có nhãn giao diện người dùng, bạn sẽ làm cho chúng 5
(tối thiểu) và 100
(tối đa).
Vì vậy, cách bố trí XML sẽ là một cái gì đó giống như ...
<LinearLayout
android:id="@+id/sampleSizeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/batchDurationSecsLayout"
android:orientation="vertical"
android:visibility="visible" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sample_size" />
<SeekBar
android:id="@+id/sampleSizeSeekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="95" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="visible" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:text="5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="right"
android:text="100" />
</RelativeLayout>
</LinearLayout>
Vì vậy tại bạn có thể giới thiệu việc sửa 5
, như thế này ...
final SeekBar sampleSizeSeekBar = (SeekBar)findViewById(R.id.sampleSizeSeekBar);
final int sampleSizeSeekBarCorrection = 5; //e.g., 95 <--> 100
int realValueFromPersistentStorage = appObj.getSampleSize(); //Get initial value from persistent storage, e.g., 100
sampleSizeSeekBar.setProgress(realValueFromPersistentStorage - sampleSizeSeekBarCorrection); //E.g., to convert real value of 100 to SeekBar value of 95.
sampleSizeSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int val = sampleSizeSeekBar.getProgress();
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
val = progress + sampleSizeSeekBarCorrection; //e.g., to convert SeekBar value of 95 to real value of 100
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
try {
appObj.saveSampleSize(val); //Save real value to persistent storage, e.g., 100
appObj.makeToast("Sample size set to " + val);
}
catch(Exception e) {
Log.e(LOG_TAG, "Error saving sample size", e);
appObj.makeToast("Error saving sample size: " + e);
}
}
});
Giải pháp này sẽ cung cấp cho thanh tìm kiếm của bạn đúng phút, tối đa và tỷ lệ trên giao diện người dùng và vị trí của điều khiển sẽ không 'nhảy' sang phải nếu người dùng trượt tất cả sang bên trái.
Bạn cũng nên gọi 'setProgress' để thay đổi được phản ánh trên giao diện người dùng - như Szabolcs Becze thể hiện trong câu trả lời của anh ấy. –
@ user1617737: Không, bạn không nên. Câu trả lời của anh ta không liên quan gì đến giá trị tối thiểu. 'SeekBar' đại diện cho phạm vi từ 0 đến (max-min). Để có được giá trị điều chỉnh, bạn thêm tối thiểu mong muốn của bạn vào tiến độ thực tế. Vì tiến trình thực tế đã được "phản ánh trên giao diện người dùng" nên không có gì thay đổi ở đó. – CommonsWare
OK, điều đó tùy thuộc vào cách thiết lập nhãn của bạn. Tôi vừa cung cấp một câu trả lời hoàn chỉnh mà tôi nghĩ là cách chính xác duy nhất để làm điều đó. http://stackoverflow.com/questions/3033135/android-seekbar-minimum-value/25239248#25239248 –