gpt4 book ai didi

android - TextView 没有及时更新

转载 作者:行者123 更新时间:2023-11-29 21:26:52 26 4
gpt4 key购买 nike

我有以下示例代码来理解 TextView 更新

public class MainActivity extends Activity 
{
int i=0;
private ImageButton btnMain;
private TextView txtText;
Context mycont=null;

public void myJob(final String cmd)
{
//txtText.setText(cmd);

runOnUiThread(new Runnable()
{
@Override
public void run() {
txtText.setText(cmd); //---Does not update the TextView here on Main UI
}
});

//----------- Long Work(Take around 15 seconds to complete) ----------
for(i=0;i<=1000000000;i++)
i++;

for(i=0;i<=1000000000;i++)
i++;
//--------------------------------------------------------------------

//---Update the TextView here once above Long work is executed
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mycont=this;

txtText = (TextView) findViewById(R.id.txtText);
txtText.setMovementMethod(new ScrollingMovementMethod());


btnMain = (ImageButton) findViewById(R.id.btnJob);
btnMain.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
myJob("Display this msg");
}
});
}
}

TextView 没有按时更新,正在等待其他过程执行。请指出我正确的方向。我想在 myJob() 函数的开头更新 TextView。

最佳答案

查看此链接:How will UI changes be processed in the onClickListener before starting a new activity in Android?

“对 UI 元素的更改不会立即执行。它们会在下次 UI 循环运行时执行,也就是在您释放控制权之后。”

关于android - TextView 没有及时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20143311/

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