单击按钮时,我需要将leftPinIndex
和rightPinIndex
变量从一个侦听器传输到另一个侦听器。
我要比现在做得更好吗?
final int[] a = new int[1];
final int[] b = new int[1];
rangebar.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListener() {
@Override
public void onRangeChangeListener(RangeBar rangeBar,
int leftPinIndex,
int rightPinIndex,
String leftPinValue,
String rightPinValue) {
//get leftPinIndex и rightPinIndex
a[0] = leftPinIndex;
b[0] = rightPinIndex;
}
resultButton.setOnClickListener(v -> {
//get variables here from rangebar listener
Log.i("variables: ", a[0] + " " + b[0]);
});
答案 0 :(得分:0)
将代码编码成一个函数,然后将数组作为参数或需要用leftPinIndex
或rightPinIndex
调用的函数传递。然后,您将访问外部事物,否则您将处于RangBar
类中,并且无法返回任何值。(通过引用调用)。
void performAction(int [] toCopyElem, Rangbar rangebar){
rangebar.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListener() {
@Override
public void onRangeChangeListener(RangeBar rangeBar,
int leftPinIndex,
int rightPinIndex,
String leftPinValue,
String rightPinValue) {
//get leftPinIndex и rightPinIndex
toCopyElem[0] = leftPinIndex;
toCopyElem[1] = rightPinIndex;
//Or call the function.
}
}