public class Main extends Activity {
int nil,nil1,nil2,nil3;
@Override
protected void onCreate(Bundle savedInstanceState) {
//sb1 for seekBar1 ... etc
//ed1 for editText1 ... etc
sbValue(sb1, ed1);
nil1 = nil; // I think I failed to get value in here
sbValue(sb2, ed2);
nil2 = nil;
sbValue(sb3, ed3);
nil3 = nil;
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.d("","nil1 = "+nil1+"nil2 = "+nil2+"nil3 = "+nil3);
}
});
}
它是sbValue Methode
public int sbValue(final SeekBar sb, final EditText ed) {
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
...
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
int val = sb.getProgress();
ed.setText(String.valueOf(val));
nil = val; //here is okay, the value is right.
}
});
//but when I tried to get Log from here, It always give me 0.
return 0;
}
}
答案 0 :(得分:0)
您正在尝试获取进度状态,并且您希望使用该功能。
您正在检查onProgressChanged方法中的Progress状态。所以你知道你只能在onProgressChanged中获得进展,那么你如何在方法之外预期进度状态或值。
无论你想要实现什么,你都应该在方法内部实现
答案 1 :(得分:0)
我明白了。我将我的变量nil更改为数组:
int[] nil = new int[3];
然后添加了一个用于描述edittext的参数。
sbValue(sb1, ed1, 1);
sbValue(sb2, ed2, 2);
sbValue(sb3, ed3, 3);
方法是这样的:
public int sbValue(final SeekBar sb, final EditText ed, final int sbSelect) {
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
...
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
int val = sb.getProgress();
ed.setText(String.valueOf(val));
switch (sbSelect){
case 1: nil[0] = val; break;
case 2: nil[1] = val; break;
case 3: nil[2] = val; break;
}
}
});
return 0;
}