我正在尝试在java应用程序(android)中做一些事情,我需要一些东西来延迟/等待循环的秒数。我怎么能延迟android功能?我曾尝试使用Thread.sleep(),TimeUnit.sleep,但它只会做几秒钟不负责任的程序。我想做一些onClick actionlistener,它会更新几秒钟。 例如:如果我点击按钮 - >文本更改为随机(int),它应该每秒完成。
随机......等待一秒......随机......等待一秒钟 ......随机......以及很多次
for (int i = 0; i < 10; i++) {
int random = r.nextInt(100) - 10;
String rand = Integer.toString(random);
textView3.setText(rand);
TimeUnit.SECONDS.sleep(1);
}
答案 0 :(得分:2)
将postDelayed
与final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Log.d("Log:", "Hello!");
handler.postDelayed(this, 1000);
}
}, 1000);
一起使用,例如:
<?php
// 1. Autoload the SDK Package. This will include all the files and classes to your autoloader
require __DIR__ . '/PayPal-PHP-SDK/autoload.php';
// 2. Provide your Secret Key. Replace the given one with your app clientId, and Secret
// https://developer.paypal.com/webapps/developer/applications/myapps
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
'AYSq3RDGsmBLJE-otTkBtM-jBRd1TCQwFf9RGfwddNXWz0uFU9ztymylOhRS', // ClientID
'EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRbL' // ClientSecret
)
);
// 3. Lets try to save a credit card to Vault using Vault API mentioned here
// https://developer.paypal.com/webapps/developer/docs/api/#store-a-credit-card
$creditCard = new \PayPal\Api\CreditCard();
$creditCard->setType("visa")
->setNumber("4417119669820331")
->setExpireMonth("11")
->setExpireYear("2019")
->setCvv2("012")
->setFirstName("Joe")
->setLastName("Shopper");
// 4. Make a Create Call and Print the Card
try {
$creditCard->create($apiContext);
echo $creditCard;
$mycard=json_decode($creditCard, true);
//echo json_encode($mycard["id"]);
$response["success"] = "1";
$response["message"] = "update success";
$response["cardid"] = $mycard["id"] ;
echo '{"worldpopulation":'.json_encode($response).'}';
}
catch (\PayPal\Exception\PayPalConnectionException $ex) {
// This will print the detailed information on the exception.
//REALLY HELPFUL FOR DEBUGGING
$response["success"] = "0";
$response["message"] = "not update success";
echo '{"worldpopulation":'.json_encode($response).'}';
//echo $ex->getData();
}
?>
答案 1 :(得分:0)
您可以使用处理程序:
for (int i = 0; i < 10; i++) {
Handler handler = new Handler();
Runnable r = new Runnable() {
public void run() {
int random = r.nextInt(100) - 10;
String rand = Integer.toString(random);
textView3.setText(rand);
}
};
handler.postDelayed(r, 1000);
}
答案 2 :(得分:0)
我对android app编程不是很熟悉
但是如果你想每一秒打印一个随机数到文本...那么使用Timer而不是Delay呢?
我不知道Android中的代码是如何工作的,但逻辑应该是这样的:
按下按钮:
Timer.Start(1000)
对于每个计时器刻度:
int numberVariable = random(1,10)
textVariable = numberVariable.toString()
答案 3 :(得分:0)
添加一个带有计时器的处理程序,如下所示:
public Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
}
}, 5000);
答案 4 :(得分:0)
您可以使用Handler类将循环延迟所需的任何时间。它是这样的。
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Your function goes here.
}
}, 5000); /your time in micro seconds.
希望有所帮助。
答案 5 :(得分:0)
实际上有很多方法可以做到这一点。
来自: https://stackoverflow.com/a/3072338/2801237
这样做的好处是你不需要处理一个处理程序,你可以很容易地随机化'5'秒输入(加上它显然是5秒),而不是5000毫秒。 (更多下面)
private static final ScheduledExecutorService worker =
Executors.newSingleThreadScheduledExecutor();
void someMethod() {
⋮
Runnable task = new Runnable() {
public void run() {
/* Do something… */
}
};
worker.schedule(task, 5, TimeUnit.SECONDS);
⋮
}
这种方法的另一大好处是你可以快速/轻松地扩展它以使用线程池。
private static final ScheduledExecutorService worker =
Executors.newScheduledThreadPool(4); //thread pool of 4 threads.
答案 6 :(得分:0)
试试这个
public void randomStart() {
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(this, 1000*1);
try {
Random r = new Random();
int random = r.nextInt(100) - 10;
String rand = Integer.toString(random);
textView3.setText(rand);
}
catch(Exception e) {
e.printStackTrace();
}
}
};
handler.postDelayed(runnable,1*1000);
}