フランク:
私は私のAndroidアプリが「オート・デモ」ボタンをので、ユーザーがクリックした後、自動デモをしたい、それがビューに切り替えると第二を遅らせ、そのビュー上のボタンをクリックし、その後、2秒後に別のボタンをクリックしてくださいその画面上..ように、このような私のJavaコードのルックス:
private class AutoDemoListener implements View.OnClickListener
{
public void onClick(View v)
{
Is_AutoDemo_B=true;
Out("AutoDemoListener");
switchView(demoView, registrationView);
startRegistration();
Thread t = new Thread(new Runnable()
{
@Override
public void run()
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
try
{
registrationView.symbolButton[2][8].performClick();
Thread.sleep(1000);
registrationView.symbolButton[4][13].performClick();
Thread.sleep(2000);
registrationView.symbolButton[0][1].performClick();
Thread.sleep(1000);
registrationView.symbolButton[6][18].performClick();
}
catch (InterruptedException e) { e.printStackTrace(); }
}
});
}
});
t.start();
Is_AutoDemo_B=false;
}
}
しかし、それは今何ん:待ち時間4秒など各クリックの間に遅延がない、一度にすべての4回のクリックをシミュレートし、それを行うための正しい方法は何ですか?
Tenfour04:
あなたは、バックグラウンドでの遅延を実行し、バックUIにするたびに結果を掲示しなければなりません。
あなたはハンドラを使用してこれを行うことができます。UIスレッドは、すでにあなたが簡単にハンドラを(他のスレッドは、よりセットアップを必要としない)を使用することができます準備ルーパーが付属しています。
ランナブルのネスティングはので、ここでそれだけ増加遅延を追加することである、厄介になります。
private class AutoDemoListener implements View.OnClickListener
{
public void onClick(View v)
{
Is_AutoDemo_B=true;
Out("AutoDemoListener");
switchView(demoView, registrationView);
startRegistration();
final Handler handler = new Handler();
registrationView.symbolButton[2][8].performClick();
handler.postDelayed(new Runnable() {
public void run() {
registrationView.symbolButton[4][13].performClick();
}
}, 1000);
handler.postDelayed(new Runnable() {
public void run() {
registrationView.symbolButton[0][1].performClick();
}
}, 3000);
handler.postDelayed(new Runnable() {
public void run() {
registrationView.symbolButton[6][18].performClick();
}
}, 5000);
handler.postDelayed(new Runnable() {
public void run() {
Is_AutoDemo_B=false;
}
}, 5100);
}
}
Kotlinでは、これはコルーチンを使用して非常にクリーンが考えられます。
val autoDemoListener = View.OnClickListener {
Is_AutoDemo_B = true
Out("AutoDemoListener")
switchView(demoView, registrationView)
startRegistration()
CoroutineScope(Job() + Dispatchers.Main).launch {
registrationView.symbolButton[2][8].performClick()
delay(1000)
registrationView.symbolButton[4][13].performClick()
delay(2000)
registrationView.symbolButton[0][1].performClick()
delay(1000)
registrationView.symbolButton[6][18].performClick()
Is_AutoDemo_B=false
}
}