Change SeekBar progress based on EditText value(根据 EditText 值更改 SeekBar 进度)
问题描述
我正在尝试根据在 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 进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:根据 EditText 值更改 SeekBar 进度
基础教程推荐
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01