我正在关注这个编写代码的人,该代码根据分配给3个不同搜索栏的RGB通道改变背景颜色,因此R为一个sk,G为另一个,G为另一个。
问题在于我无法使代码工作:
private int seekR, seekG, seekB;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_changer);
SeekBar sbR = (SeekBar) findViewById(R.id.RedSeekBar);
SeekBar sbG = (SeekBar) findViewById(R.id.GreenSeekBar);
SeekBar sbB = (SeekBar) findViewById(R.id.BlueSeekBar);
mySeekBarChangeListener msbclR = new mySeekBarChangeListener();
mySeekBarChangeListener msbclG = new mySeekBarChangeListener();
mySeekBarChangeListener msbclB = new mySeekBarChangeListener();
sbR.setonseekBarChangeListener(msbclR);
sbG.setonseekBarChangeListener(msbclG);
sbB.setonseekBarChangeListener(msbclB);
msbclR.onprogressChanged(sbR, sbR.getProgress(), false);
msbclG.onprogressChanged(sbG, sbG.getProgress(), false);
msbclB.onprogressChanged(sbB, sbB.getProgress(), false);
}
这篇文章几乎无处不在给我一个错误,无论如何,似乎没什么用。我只是想知道为什么......我必须根据mySeekBarChangeListener设置一些东西吗?提前谢谢!
答案 0 :(得分:4)
应该有效:
private int seekR, seekG, seekB;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_changer);
SeekBar sbR = (SeekBar) findViewById(R.id.RedSeekBar);
SeekBar sbG = (SeekBar) findViewById(R.id.GreenSeekBar);
SeekBar sbB = (SeekBar) findViewById(R.id.BlueSeekBar);
sbR.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekR = progress;
doSomethingWithColor();
}
});
sbG.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekG = progress;
doSomethingWithColor();
}
});
sbB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekB = progress;
doSomethingWithColor();
}
});
}
private void doSomethingWithColor() {
int color = Color.rgb(seekR, seekG, seekB);
// Do something with color
}
或更紧凑
private int seekR, seekG, seekB;
private SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
switch (seekBar.getId()) {
case R.id.RedSeekBar:
seekR = progress;
break;
case R.id.GreenSeekBar:
seekG = progress;
break;
case R.id.BlueSeekBar:
seekB = progress;
break;
}
doSomethingWithColor();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_changer);
SeekBar sbR = (SeekBar) findViewById(R.id.RedSeekBar);
SeekBar sbG = (SeekBar) findViewById(R.id.GreenSeekBar);
SeekBar sbB = (SeekBar) findViewById(R.id.BlueSeekBar);
sbR.setOnSeekBarChangeListener(onSeekBarChangeListener);
sbG.setOnSeekBarChangeListener(onSeekBarChangeListener);
sbB.setOnSeekBarChangeListener(onSeekBarChangeListener);
}
private void doSomethingWithColor() {
int color = Color.rgb(seekR, seekG, seekB);
// Do something with color
}
另一个
private int seekR, seekG, seekB;
public class MySeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
switch (seekBar.getId()) {
case R.id.RedSeekBar:
seekR = progress;
break;
case R.id.GreenSeekBar:
seekG = progress;
break;
case R.id.BlueSeekBar:
seekB = progress;
break;
}
doSomethingWithColor();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_changer);
SeekBar sbR = (SeekBar) findViewById(R.id.RedSeekBar);
SeekBar sbG = (SeekBar) findViewById(R.id.GreenSeekBar);
SeekBar sbB = (SeekBar) findViewById(R.id.BlueSeekBar);
MySeekBarChangeListener msbclR = new MySeekBarChangeListener();
MySeekBarChangeListener msbclG = new MySeekBarChangeListener();
MySeekBarChangeListener msbclB = new MySeekBarChangeListener();
sbR.setOnSeekBarChangeListener(msbclR);
sbG.setOnSeekBarChangeListener(msbclG);
sbB.setOnSeekBarChangeListener(msbclB);
}
private void doSomethingWithColor() {
int color = Color.rgb(seekR, seekG, seekB);
// Do something with color
}