java - 实现设置一些 TextView 属性的方法

标签 java android

我需要实现一个自定义方法来根据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/

相关文章:

java - 具体类使用 mockito 模拟接口(interface)

java - JVM cucumber : Running multiple testcase on a single step definition

android - 基于软件的卡仿真 (HCE) 如何保证 NFC 安全?

java - 当方向改变时将对象从一个 fragment 传递到另一个 fragment

java - 如何: Docker container running java jar connecting to host mysql server?

java - HTTP 直播流在 JavaFX 应用程序中不起作用

android - Proguard 吃了我的 Object.wait()

java - Android 构建路径条目丢失

安卓编辑文本 : Get LineCount in an Activity's onCreate()

Android Studio 1.4.0 Drawable-v21 项被忽略以支持 Drawable