gpt4 book ai didi

Android:在计算机控制的 UI 更改期间更新屏幕

转载 作者:行者123 更新时间:2023-11-29 22:30:10 25 4
gpt4 key购买 nike

我已经检查了这里提出的其他问题,但没有成功实现解决方案。我的应用程序使用控件,而不是在 Canvas 上绘图。基本上这总结了我的问题:-

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (ToggleButton) findViewById(R.id.btn1);
btn2 = (ToggleButton) findViewById(R.id.btn2);
btn3 = (ToggleButton) findViewById(R.id.btn2);
btn4 = (ToggleButton) findViewById(R.id.btn4);
btn5 = (ToggleButton) findViewById(R.id.btn5);
btn6 = (ToggleButton) findViewById(R.id.btn6);
btn7 = (ToggleButton) findViewById(R.id.btn7);
//blah blah blah
btn1.requestFocus();
wait(100);
btn2.requestFocus();
wait(100);
btn3.requestFocus();
wait(100);
btn3.setChecked(true);
wait(100);
btn4.requestFocus();
wait(100);
btn4.setChecked(true);
wait(100);
btn5.requestFocus();
wait(100);
btn6.requestFocus();
wait(100);
btn6.setChecked(true);
wait(100);
btn7.requestFocus();
wait(100);
//etc
}

要点是计算机动画移动到按钮并检查它们,以便用户可以看到它在做什么。所以,我的问题是,暂停 100 毫秒并更新屏幕和焦点的 wait() 方法中发生了什么?非常感谢任何帮助,乔纳森干杯。

解决方案适用,谢谢 Andrew :)

int timerCounterInterval = 0;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ToggleButton btn1 = (ToggleButton) findViewById(R.id.btn1);
final ToggleButton btn2 = (ToggleButton) findViewById(R.id.btn2);
// more btns
timerCounterInterval = 0; //reset, for each pass of automation
setFocus(btn1);
setFocus(btn2);
setChecked(btn2);
// tidy up
timerCounterInterval++;
new Handler().postDelayed(new Runnable() {
public void run() {
btn2.clearFocus(); //whatever button that had the focus last
}
}, 100*(timerCounterInterval));
// etc - note any code from here will execute *before* the above timer
}

public void setFocus(final ToggleButton btn) {
timerCounterInterval++;
new Handler().postDelayed(new Runnable() {
public void run() {
btn.setFocusableInTouchMode(true); //eww, messy i know
btn.requestFocus();
btn.setFocusableInTouchMode(false);
}
}, 100*timerCounterInterval);
}

public void setChecked(final ToggleButton btn) {
timerCounterInterval++;
new Handler().postDelayed(new Runnable() {
public void run() {
btn.setChecked(true);
}
}, 100*timerCounterInterval);
}

希望这对其他人有帮助!

最佳答案

我强烈建议您查看 Handle对象,特别是 postDelayed方法。这样您就可以将所有内容都保留在 UI 线程上,而不会导致线程实际上被绑定(bind)在动画方法中。这也可以防止 Activity 发出可怕的“此应用程序未响应”消息。

事实上,只需将对控件的 setter 的调用包装在一个 runnable 中并将它们传递给 postDelayed 就足以更新 GUI 控件。

[更新]作为根据您的评论的示例,您可以尝试这样的事情。我还没有真正测试过这个,但我认为这个想法仍然存在......

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ToggleButton btn1 = (ToggleButton) findViewById(R.id.btn1);
final ToggleButton btn2 = (ToggleButton) findViewById(R.id.btn2);
// more btns

Handler handler = new Handler();

/* tell the handler run these bits after 1 sec, 2 sec, 3 sec, ect... */
handler.postDelayed(new Runnable() { void run () { btn1.requestFocus(); } }, 1000);
handler.postDelayed(new Runnable() { void run () { btn2.requestFocus(); } }, 2000);
handler.postDelayed(new Runnable() { void run () { btn2.setChecked(true); } }, 3000);
}

关于Android:在计算机控制的 UI 更改期间更新屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4351154/

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