gpt4 book ai didi

java - Android TextView设置文本和颜色

转载 作者:行者123 更新时间:2023-12-01 17:30:24 25 4
gpt4 key购买 nike

我想在onCreate方法中随机设置文本的大小和颜色

这是我的代码:

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


}

我的这段代码不起作用。

在 onCreate 期间,我希望文本大小和颜色连续且随机地改变。

最佳答案

它对我有用:(如果有问题请告诉我)

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



}

关于java - Android TextView设置文本和颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11622926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com