問題描述
我正在嘗試根據在 EditText
中輸入的數字來更改 SeekBar
的進度,但由于某種原因 EditText
值變為到最大,我無法在 SeekBar
中滑動拇指.
I am trying to change the progress of a SeekBar
based on the number entered in an EditText
but for some reason the EditText
value goes to the max and I can't slide the thumb in the SeekBar
.
我希望實現的目標:如果在 EditText
中輸入的值介于 70 和 190 之間(包括兩個數字),則將 SeekBar
的進度更改為該值.
What I am looking to achieve: If the value entered in the EditText
anywhere between 70 and 190 (including both number) change the progress of the SeekBar
to that value.
部分 Java 代碼:
Partial Java code:
etOne = (EditText) findViewById(R.id.etSyst);
etOne.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
String filtered_str = s.toString();
if (Integer.parseInt(filtered_str) >= 70 && Integer.parseInt(filtered_str) <= 190) {
sbSyst.setProgress(Integer.parseInt(filtered_str));
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
部分 XML:
<SeekBar
android:id="@+id/syst_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:progress="0"
android:max="120"
android:progressDrawable="@drawable/progress_bar"
android:secondaryProgress="0"
android:thumb="@drawable/thumb_state" />
我給每個值加 70,因為 SeekBar
從 0 開始,但我希望它從 70 開始.
I add 70 to each value, because SeekBar
starts at 0, but I want it to start at 70.
使用上面的代碼后是這樣的:
After using the above code, this is what it looks like:
SeekBar
是最大數,EditText
也是最大數.
The SeekBar
is at the maximum number and the EditText
is at the maximum as well.
推薦答案
etOne = (EditText) findViewById(R.id.etSyst);
etOne.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
int i = Integer.parseInt(s.toString());
if (i >= 70 && i <= 190) {
sbSyst.setProgress( i - 70); // This ensures 0-120 value for seekbar
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
這篇關于根據 EditText 值更改 SeekBar 進度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!