简体   繁体   中英

Android TextView setting text and color

I would like to set the size and color of the text at random during onCreate method

Here is my code:

private TextView start;
private boolean isClicked;
protected void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    start = (TextView) findViewById(R.id.tvStart);

    isclick = false;
    Random r = new Random();

    while (isclick = false)
    {

        start.setTextSize(r.nextInt(50));
        start.setTextColor(Color.rgb(r.nextInt(256), r.nextInt(256),
                r.nextInt(256)));
    }


}

This code of mine doesn't work.

During onCreate I want the text size and color continuously and randomly changing.

It works for me :( let me know is there is a problem )

private boolean isclick;
Handler handler ; 
private TextView start;
private boolean isClicked;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        start = (TextView) findViewById(R.id.tvStart);
        handler = new Handler();

        isclick = false;
        new Thread(new Runnable() {

            @Override
            public void run() {
                 while (isclick == false)
                    {
                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        Random r = new Random();



                            start.setTextSize(r.nextInt(50));
                            start.setTextColor(Color.rgb(r.nextInt(256), r.nextInt(256),
                                    r.nextInt(256)));

                        }

                    });
                     Log.w("DEBUG","Text View  value : "+ start.getText().toString());

                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                }


            }
        }).start();



    }

你需要使用一个处理程序的罪你正在修改UI部件文章可以帮助你。

Here is some code that works on a TextView called x

//create random value between 0 and 70
int random = (int)Math.ceil(Math.random()*70);
x.setTextSize((float)random);

int red = (int)Math.ceil(Math.random()*255);
int green = (int)Math.ceil(Math.random()*255);
int blue = (int)Math.ceil(Math.random()*255);

Color randomcolor = new Color();

if (red < 16){
    hexred = "0" + Integer.toHexString(red);
}else {
    hexred = Integer.toHexString(red);
}
if (green < 16){
    hexgreen = "0" + Integer.toHexString(green);
}else {
    hexgreen = Integer.toHexString(green);
}
if (blue < 16){
    hexblue = "0" + Integer.toHexString(blue);
}else {
    hexblue = Integer.toHexString(blue);
}

String color = "#" + hexred + hexgreen + hexblue;

x.setTextColor(randomcolor.parseColor(color));

Continuasly changing is not recommended however. If you want to make it continuasly change color, chance is that the update of the color and size are to slow that nothing is displayed. Also it might happen that the entire XML layout is not loaded because of the calculations on the xml.

if you want your text randomly changing continously, use thread.
put your "while" action. inside run method, and give some delay on it.

I think its because of a missing = in the isclick = false if clause. It should be isclick==false .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM