创建时未正确设置textview颜色

rslzwgfq  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(220)

所以我在按下一个包含40x30(大致)二维字符Map的按钮后加载了一个新的布局:

map = new char[][]{
            "...........,,,,,,.........................".toCharArray(),
            ",........,,,,,,,,,,H,.....................".toCharArray(),
            ",,,......V.,,,,,,,..........F.............".toCharArray(),
            ",,,,,.....,,,,,,,....................H....".toCharArray(),
            ",,,,,,..,,,.....H.........................".toCharArray(),
            ",,,,,,,,,..............................F..".toCharArray(),
            ",,,,,,,,,,,,...................;;;;;;.....".toCharArray(),
            ",,,,,,,,,,............K......;;;;V;;;;;...".toCharArray(),
            ",,,,,,,V,,,,................;;;;;;;;;;;;;.".toCharArray(),
            ",,,,,,,,........H.............;;;;;;;;;;;.".toCharArray(),
            ",,,,,,,.........................;;;;;;;;;.".toCharArray(),
            "......H.........IM.........TT.....;;;;;;;;".toCharArray(),
            "..............MM..........TTT.....;;;;;;;;".toCharArray(),
            "............KMM.....A..H.TTTTT....H;;;;;;;".toCharArray(),
            "......F.......MM...........TTTTT......;;..".toCharArray(),
            "............MMK.............TT.......;;;..".toCharArray(),
            "............MMM.....H................;;;..".toCharArray(),
            ".....H.........MM..................;;;;;..".toCharArray(),
            "...............CM...............;;;;;;V...".toCharArray(),
            "..F..........................;;;;;;;H;;;..".toCharArray(),
            ".....................;;;;;;;;;;;;;;;;;;;..".toCharArray(),
            "..................H......;;;;;;;;;;;;;;...".toCharArray(),
            ".......V.....................;;;;;;;......".toCharArray(),
            "......,,,,,,,,.............H..TTTTT.......".toCharArray(),
            "...,,,,,,,,,,,,...........TTTTTTTTTTT.....".toCharArray(),
            ",,F,,,.....,,,,,,........TTT....TT...TTT..".toCharArray(),
            ",,,,.........,,,........TT....MMM..MMMK...".toCharArray(),
            ",,,,,,.....,,,,,,,.V....TTT....MMSMMMM....".toCharArray(),
            "..,,,F,,,,,,,,,..............MMMM..MMMM...".toCharArray(),
            "....,,,,,,,,,.............................".toCharArray()
    };

下面的函数将根据关键点坐标列表,将关键点涂成黑色,而将其余的点涂成浅灰色。现在,它只是着色的第一个或最后一个关键点。

private void drawMap(){
    for(int x = 0; x < map.length; x++){
        TableRow tr = new TableRow(getBaseContext());
        for(int y = 0; y < map[x].length; y++){
            TextView tv = new TextView(getBaseContext());
            tv.setTextSize(18.0f);
            System.out.println("CHECKING " + x + ", " + y);
            for (int[] keyPoint : keyPoints) {

                System.out.println("KEYPOINT " + keyPoint[0] + ", " + keyPoint[1]);
                if (x == keyPoint[0] && y == keyPoint[1]){
                    tv.setTextColor(Color.BLACK);
                    System.out.println("BLACK");
                    break;
                } else{
                    System.out.println("GREY");
                    tv.setTextColor(Color.LTGRAY);
                    break;
                }

            }
            tv.setTypeface(Typeface.MONOSPACE);
            tv.setText(Character.toString(map[x][y]));

            tr.addView(tv);
        }
        mapTbl.addView(tr);
    }
}

以及要点列表:

keyPoints = new int[][]{
            {1, 19},
            {2, 9},
            {2, 28},
            {3, 37},
            {4, 16},
            {5, 39},
            {7, 22},
            {7, 33},
            {8, 7},
            {9, 16},
            {11, 6},
            {11, 16},
            {13, 12},
            {13, 20},
            {13, 23},
            {13, 34},
            {14, 6},
            {15, 14},
            {16, 20},
            {17, 5},
            {18, 15},
            {18, 38},
            {19, 3},
            {19, 36},
            {21, 18},
            {22, 7},
            {23, 27},
            {25, 2},
            {26, 38},
            {27, 19},
            {27, 33},
            {28, 5},
    };

任何和所有的反馈都是非常宝贵的。

laawzig2

laawzig21#

这个for循环总是中断。所以它不会循环每个关键点。

for (int[] keyPoint : keyPoints) {

            System.out.println("KEYPOINT " + keyPoint[0] + ", " + keyPoint[1]);
            if (x == keyPoint[0] && y == keyPoint[1]){
                tv.setTextColor(Color.BLACK);
                System.out.println("BLACK");
                break;
            } else{
                System.out.println("GREY");
                tv.setTextColor(Color.LTGRAY);
                break;
            }

解决这个问题的一个方法是执行以下操作。

private void drawMap(){
    for(int x = 0; x < map.length; x++){
        TableRow tr = new TableRow(getBaseContext());
        for(int y = 0; y < map[x].length; y++){
            TextView tv = new TextView(getBaseContext());
            tv.setTextSize(18.0f);
            System.out.println("CHECKING " + x + ", " + y);

            boolean keyPointFound = false;

            for (int[] keyPoint : keyPoints) {
                System.out.println("KEYPOINT " + keyPoint[0] + ", " + keyPoint[1]);
                if (x == keyPoint[0] && y == keyPoint[1]){
                    tv.setTextColor(Color.BLACK);
                    System.out.println("BLACK");
                    keyPointFound = true;
                    break;
                }
            }
            if(!keyPointFound) {
                System.out.println("GREY");
                tv.setTextColor(Color.LTGRAY);
            }
            tv.setTypeface(Typeface.MONOSPACE);
            tv.setText(Character.toString(map[x][y]));

            tr.addView(tv);
        }
        mapTbl.addView(tr);
    }
}

else语句从keypoint循环中删除,我添加了boolean keypointfound。如果未找到关键点,则文本视图为灰色。
更新的代码创建了这个。

相关问题