我有两个字符串变量叫做clockedin和clockedout。我把它们放在共享首选项中,现在我正在尝试调用它们并将它们转换为整数,这样我就可以从clockedin小时中减去clockedout小时数来查看工作了多少小时。
public void onCheckedChanged(CompoundButton compoundButton, boolean b){
if(b){
Calendar c = Calendar.getInstance();
System.out.println(c.getTime());
SimpleDateFormat df = new SimpleDateFormat("hh:mm a");
String formattedDate = df.format(c.getTime());
TextView clockedin = (TextView)findViewById(R.id.clockedin);
clockedin.setText(formattedDate);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("toggleButton", tb.isChecked());
editor.commit();
SharedPreferences sharedPreferences2=getSharedPreferences("MyData", Context.MODE_PRIVATE);
SharedPreferences.Editor editor1= sharedPreferences2.edit();
editor1.putString("clockedin", formattedDate);
editor1.commit();
}
else {
Calendar c = Calendar.getInstance();
System.out.println(c.getTime());
SimpleDateFormat df = new SimpleDateFormat("hh:mm a");
String formattedDate = df.format(c.getTime());
TextView clockedout = (TextView)findViewById(R.id.clockedout);
clockedout.setText(formattedDate);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("toggleButton", false);
editor.commit();
SharedPreferences sharedPreferences2=getSharedPreferences("MyData2", Context.MODE_PRIVATE);
SharedPreferences.Editor editor1= sharedPreferences2.edit();
editor1.putString("clockedout", clockedout.getText().toString());
editor1.commit();
SharedPreferences sharedPreferences1 = getSharedPreferences("MyData", Context.MODE_PRIVATE);
String clockedin =sharedPreferences1.getString("clockedin", DEFAULT);
}}
我的转换方法:
public void onClickButtonListener2(){
Button button2;
button2 = (Button) findViewById(R.id.calctotal);
button2.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Date date = null;
Date date2 = null;
SharedPreferences sharedPreferences1 = getSharedPreferences("MyData", Context.MODE_PRIVATE);
String clockedin =sharedPreferences1.getString("clockedin", DEFAULT);
SharedPreferences sharedPreferences2 = getSharedPreferences("MyData2", Context.MODE_PRIVATE);
String clockedout =sharedPreferences2.getString("clockedout",DEFAULT);
DateFormat outFormat = new SimpleDateFormat("hh:mm");
try {
date = outFormat.parse(String.valueOf(clockedout));
date2 = outFormat.parse(clockedin);
}catch ( ParseException e){
e.printStackTrace();
}
if (date2.before(date)){
long diffMs = date.getTime() - date2.getTime();
long diffSec = diffMs/1000;
long min = diffSec/60;
double hour = (double)min/60;
double hourA = Math.round(hour *100.0)/100.0;
TextView totalhours=(TextView)findViewById(R.id.totalhours);
totalhours.setText((int) hourA);
}
}
});
}
我的日志已加入enter image description here
答案 0 :(得分:1)
totalhours.setText((int)hourA);
应该是:totalhours.setText(String.valueOf(hourA)); 要么 totalhours.setText(“”+ hourA);
如果使用整数调用setText,它将查找具有该ID的字符串资源。
答案 1 :(得分:0)
为什么要存储格式化日期而不是Calendar.getInstance()中的long值?只是做
if(..){
editor.putLong("dateIn", Calendar.getInstance().getTimeInMillis());
}
else{
editor.putLong("dateIn", Calendar.getInstance().getTimeInMillis());
}
然后将它们作为long来减去
long dateIn = sharedPreferences.getLong("dateIn", defaultValueIn);
long dateOut = sharedPreferences.getLong("dateOut", defaultValueOut);
long difference = dateOut - dateIn;
int hoursBetween = difference/(1000*60*60)
现在你刚刚保存了两页代码