Java 我有一个叫做打字游戏的应用程序,它';s检查文本并计算时间?
我在onCreate方法中使用了此选项,当我在对话框开始时单击ok:Java 我有一个叫做打字游戏的应用程序,它';s检查文本并计算时间?,java,android,methods,timer,Java,Android,Methods,Timer,我在onCreate方法中使用了此选项,当我在对话框开始时单击ok: private static final int Ready_Dialog=1; private static final int CORRECT_DIALOG = 2; private static final int INCORRECT_DIALOG = 3; private static double startTime = System.currentTimeMillis(); private stat
private static final int Ready_Dialog=1;
private static final int CORRECT_DIALOG = 2;
private static final int INCORRECT_DIALOG = 3;
private static double startTime = System.currentTimeMillis();
private static double endTime = System.currentTimeMillis();
showDialog(Ready_Dialog);
public void onClick(View arg0) {
EditText et=(EditText)findViewById(R.id.editText);
String input=et.getText().toString();
TextView tv=(TextView)findViewById(R.id.textView1);
String except=tv.getText().toString();
if(input.equals(except))
{
removeDialog(CORRECT_DIALOG);
endTime=System.currentTimeMillis();
showDialog(CORRECT_DIALOG);
}
else
{
removeDialog(INCORRECT_DIALOG);
showDialog(INCORRECT_DIALOG);
}
}
});
受保护的对话框onCreateDialog(int id){
双倍时间
String Message = null;
if (id == Ready_Dialog){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// this is the message to display
builder.setMessage(R.string.ready);
// this is the button to display
builder.setPositiveButton(R.string.yes,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int id){
// this will hide the dialog
startTime=System.currentTimeMillis();
dialog.cancel();
}
});
return builder.create();
}
if(id==INCORRECT_DIALOG)
Message="You get it wrong !Click Yes to play again ";
else
if(id==CORRECT_DIALOG)
{
time=((endTime-startTime/100)/10);
BigDecimal db = new BigDecimal(time).setScale(2, BigDecimal.ROUND_HALF_UP);
Message="That's right ! That took you"+time+" seconds! Click Yes to play again.";
}
AlertDialog.Builder builder=new AlertDialog.Builder(this);
AlertDialog.Builder builder1=new AlertDialog.Builder(this);
builder.setMessage(Message);
builder1.setPositiveButton(R.string.no, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface arg0, int arg1){
// close App
finish();
}
});
但显示为second的对话框会查看相关图片
就像当你点击“提交”按钮时,Toast消息会说“是的,它花了你3.2秒”作为例子。你要做的是设置你的精度。下面我告诉它找到一半,只显示两个小数点。如果你想要更多,请将2改为其他值
double time =((endTime-startTime/100)/10);
BigDecimal db = new BigDecimal(time).setScale(2, BigDecimal.ROUND_HALF_UP);
您可能希望查看,以查看使用
BigDecimal
可以执行的所有其他有趣的操作。问题是您将“startTime”除以100,而不是“endTime”。请尝试:
double time = ((endTime - startTime) / 1000);
您可能还想在Java中搜索打印浮点数,以便正确设置格式。您可以试试这个简单的技巧
int time = ((endTime-startTime/100)/10) *10;
无论何时要显示时间,请执行此操作
(float) (time /10)
例如,如果时间是3.5xxxx(x是我们不关心和丢弃的数字)
第一行将35指定为时间,第二行将始终返回3.5。当您初始化时间时,其余数字将被丢弃我希望此应用程序以这样的方式显示时间,3.15秒在pic中不一样。有什么解决方法吗?我想他正在试图弄清楚如何设置数字格式:-)@user3025186请参阅下面的答案。如果您有同样的问题,你没有告诉我们我们需要补偿。你能提供更多的代码吗?请看更多的代码@buzzsawdogthanks这么多@buzzsawdog