实现一个方法来设置一些TextView属性

时间:2016-08-21 04:13:21

标签: java android

我需要实现一个自定义方法,根据搜索栏的位置切换一些TextView背景颜色(第一个和最后一个位置除外,它们是固定的)。

我需要对10个视图进行操作。所以我创建了这个类SwitchColor来尝试自动化我的工作。构造函数使用4个参数:TextView我想要更改背景,搜索栏位置,初始颜色和最终颜色。

我不确定如何从新对象SwitchTextViewBackground()调用内部方法SwitchColor

我的主要活动:

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);
        }
    }
}}

1 个答案:

答案 0 :(得分:1)

像这样编辑你的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) {

                }

            }
    );

}}