我想推迟选定的延迟时间的烤面包一样(15,30,60秒,无延迟),但它不会工作。这里的code:
I want to delay the toast on selected delay times like (15, 30, 60 seconds and no delay) but it won't work. Here's the code:
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
if(FirstLoad){
FirstLoad = false;
return;
}
Toast.makeText(parent.getContext(), "You chose " +
parent.getItemAtPosition(pos).toString()+ " to delay", Toast.LENGTH_LONG).show();
Message message = new Message();
Bundle bun = new Bundle();
bun.putString("delay", parent.getItemAtPosition(pos).toString());
message.obj = bun;
if (pos == 0) {
handler.sendMessageDelayed(message, 0);
}
else if (pos == 1) {
handler.sendMessageDelayed(message, 15000);
}
else if (pos == 2) {
handler.sendMessageDelayed(message, 30000);
}
else if (pos == 3) {
handler.sendMessageDelayed(message, 60000);
}
//handler.sendMessageDelayed(message, 15000);
}
public void onNothingSelected(AdapterView<?> parent) {
return;
}
请帮助。
试试这个:
final Toast toast = Toast.makeText(parent.getContext(), "You chose "
+ parent.getItemAtPosition(pos).toString() + " to delay",
Toast.LENGTH_LONG);
Runnable showToastRunnable = new Runnable() {
public void run() {
toast.show();
}
};
if (pos == 0) {
handler.postDelayed(showToastRunnable, 0);
} else if (pos == 1) {
handler.postDelayed(showToastRunnable, 15000);
} else if (pos == 2) {
handler.postDelayed(showToastRunnable, 30000);
} else if (pos == 3) {
handler.postDelayed(showToastRunnable, 60000);
}
编辑:
顺便说一句,我想转让这对发送按钮,我想延缓根据用户选择的延迟消息发送致祝酒辞。我应该如何实现呢?
By the way, I want to transfer this to the send button, i want to delay the toast of "Message sent" according to the delay the user chose. How should I implement it?
你是如何获取的延迟?它说的是用户输入在的EditText
?
在这种情况下,你可以只得到这样的延迟:
How are you fetching the delay? Is it something the user enters in an EditText
?
In that case you could just get the delay like this :
int delay = Integer.parseInt(delayEditText.getText().toString());
,然后使用该延迟量后可运行的处理程序是这样的:
and then use that delay amount to post the runnable to the handler like this :
handler.postDelayed(showToastRunnable, delay);
您可以删除在这种情况下,你的整个的if-else块。
You can remove your entire if-else block in this case.