我需要实现一个自定义方法来根据seekbar的位置切换一些TextView
背景颜色(除了第一个和最后一个位置,它们是固定的)。
我需要做到 10 次观看。所以我创建了这个类 SwitchColor
来尝试自动化我的工作。构造函数使用 4 个参数:TextView
我想要更改背景、搜索栏位置、初始颜色和最终颜色。
我不确定如何从新对象 SwitchColor
调用内部方法 SwitchTextViewBackground()
。
我的主要 Activity :
public class MainActivity extends AppCompatActivity {
private static SeekBar seekbar;
private static TextView textview;
private static TextView topSqr;
private static TextView middleSqr;
private static TextView middleSqrContentA;
private static TextView MiddleSqrContentB;
private static TextView bottomSqr;
private static TextView stripe1;
private static TextView stripe2;
private static TextView stripe3;
private static TextView stripe4;
private static TextView stripe5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBarAction();
}
private void seekBarAction() {
seekbar = (SeekBar) findViewById(R.id.seekBar);
seekbar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
int seekBarProgressValue = 0;
SwitchColor switchColor = new SwitchColor(topSqr,seekBarProgressValue,"#FFF","#000");
switchColor.SwitchTextViewBackground(); // cannot resolve Symbol
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
seekBarProgressValue = i;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
}}
还有我的自定义类:
public class SwitchColor{
private TextView view;
private int colorIndex;
private String initialColor;
private String finalColor;
public SwitchColor(TextView view, int colorIndex, String initialColor, String finalColor) {
this.view = view;
this.colorIndex = colorIndex;
this.initialColor = initialColor;
this.finalColor = finalColor;
}
public void SwitchTextViewBackground(){
if(colorIndex <= 10 && colorIndex < 0) {
view.setBackgroundColor(Color.parseColor(initialColor));
} else {
if (colorIndex <= 90 && colorIndex <=100){
view.setBackgroundColor(Color.parseColor(finalColor));
}
else {
Random rnd = new Random();
int color = Color.argb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
view.setBackgroundColor(color);
}
}
}}
最佳答案
像这样编辑你的 SwithchColor 类
public class SwitchColor{
private TextView view;
private int colorIndex;
private String initialColor;
private String finalColor;
public SwitchColor(TextView view, int colorIndex, String initialColor, String finalColor) {
this.view = view;
this.colorIndex = colorIndex;
this.initialColor = initialColor;
this.finalColor = finalColor;
SwitchTextViewBackground();//Change is here
}
public void SwitchTextViewBackground(){
if(colorIndex <= 10 && colorIndex < 0) {
view.setBackgroundColor(Color.parseColor(initialColor));
} else {
//if (colorIndex <= 90 && colorIndex <=100){
if (colorIndex >= 90 && colorIndex <=100){//Here is an edit
view.setBackgroundColor(Color.parseColor(finalColor));
}
else {
Random rnd = new Random();
int color = Color.argb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
view.setBackgroundColor(color);
}
}
}
}
然后像这样在seekBarAction中编辑
private void seekBarAction() {
seekbar = (SeekBar) findViewById(R.id.seekBar);
seekbar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
//int seekBarProgressValue = 0;
//SwitchColor switchColor = new SwitchColor(topSqr,seekBarProgressValue,"#FFF","#000");
// switchColor.SwitchTextViewBackground(); // cannot resolve Symbol
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
//seekBarProgressValue = i;
//Edit is here
SwitchColor switchColor = new SwitchColor(topSqr,i,"#FFFFFF","#000000");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
}}
关于java - 实现设置一些 TextView 属性的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39060783/