[英]How to formats the min and max value of android-range-seek-bar ?
I use the control android-range-seek-bar at https://github.com/anothem/android-range-seek-bar to show two thumb controls allowing a range to be selected. 我在https://github.com/anothem/android-range-seek-bar上使用控制android-range-seek-bar来显示两个拇指控件,允许选择范围。
I hope to the Text indicating the selected min and max values on the bar are formatted as "hh:mm:ss", I have created a function ConvertSecondsToHMmSs
, how can I do that? 我希望文本指示栏上选定的最小值和最大值格式为“hh:mm:ss”,我创建了一个函数
ConvertSecondsToHMmSs
,我该怎么做? Such as 1000L disply "00:00:01" 如1000L显示“00:00:01”
And more, I hope to do something when min or max value changed, but there is only the event public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Long minValue, Long maxValue)
, it can't differentiate which value, min or max value, is changed. 而且,我希望在最小值或最大值更改时执行某些操作,但只有事件
public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Long minValue, Long maxValue)
,它无法区分哪个值,最小值或最大值,改变了。 I hope to do A when min value changed, and do B when max value changed! 我希望在最小值改变时做A ,并在最大值改变时做B !
Code 码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RangeSeekBar<Long> seekBar = (RangeSeekBar<Long>) findViewById(R.id.myRangeSeekBar);
seekBar.setRangeValues(1000L,2000L);
}
private String ConvertSecondsToHMmSs(long millis) {
return String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
}
Layout 布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:rsb="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<org.florescu.android.rangeseekbar.RangeSeekBar
android:id="@+id/myRangeSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
rsb:absoluteMaxValue="150"
rsb:absoluteMinValue="20"/>
</RelativeLayout>
build.gradle 的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.cuiwei.myapplication"
minSdkVersion 21
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'org.florescu.android.rangeseekbar:rangeseekbar-library:0.3.0'
}
That's fairly simple, though you'll need to modify the original RangeSeekBar
: 这很简单,但你需要修改原始的
RangeSeekBar
:
1. 1。
Copy RangeSeekBar
class into your project 将
RangeSeekBar
类复制到您的项目中
Add your convertSecondsToHMmSs
into your custom RangeSeekBar
class 将
convertSecondsToHMmSs
添加到自定义RangeSeekBar
类中
In onDraw
method of RangeSeekBar
: 在
RangeSeekBar
onDraw
方法中:
replace (lines 646-647) 替换(第646-647行)
String minText = String.valueOf(getSelectedMinValue()); String maxText = String.valueOf(getSelectedMaxValue());
with 同
String minText; String maxText; if (getSelectedMinValue() instanceof Long && getSelectedMaxValue() instanceof Long) { minText = String.valueOf(convertSecondsToHMmSs((Long)getSelectedMinValue())); maxText = String.valueOf(convertSecondsToHMmSs((Long)getSelectedMaxValue())); } else { minText = String.valueOf(getSelectedMinValue()); maxText = String.valueOf(getSelectedMaxValue()); }
(of course, this can be generalized for broader use!) (当然,这可以推广到更广泛的用途!)
2. To distinguish min/max border change - just store the previous values and compare them: 2.区分最小/最大边界变化 - 只需存储先前的值并进行比较:
Long currentMin = 1000L, currentMax = 2000L;
......
RangeSeekBar rangeSeekBar = (RangeSeekBar)findViewById(R.id.myRangeSeekBar);
rangeSeekBar.setRangeValues(currentMin, currentMax);
rangeSeekBar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener() {
@Override
public void onRangeSeekBarValuesChanged(RangeSeekBar bar, Object minValue, Object maxValue) {
if (!currentMin.equals(minValue)) {
Toast.makeText(MainActivity.this, "Min border changed", Toast.LENGTH_SHORT).show();
}
if (!currentMax.equals(maxValue)) {
Toast.makeText(MainActivity.this, "Max border changed", Toast.LENGTH_SHORT).show();
}
currentMin = (Long)minValue;
currentMax = (Long)maxValue;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.