以下是我的代码。根据我的APp,我可以通过单击相应的按钮进入从DAte和ToDAte。但是从datepicker对话框中选择日期后,我必须找到天之间的差异。然后计算总成本。但是我的代码有问题,因为我的FromDate和ToDate采用相同的日期,因此仅将总成本显示为零。我是Android新手。请帮我解决这个问题。
public class MainActivity extends AppCompatActivity {
TextView tv_rentcarheading, tv_costresult;
Button btndateFrom, btndateTo, btncalculate;
int CurrentYear1, CurrentMonth1, CurrentDay1, CurrentYear2, CurrentMonth2, CurrentDay2;
final int rent = 150;
static final int dialog_id1 = 0, dialog_id2 = 0;
long n, totalcost;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_rentcarheading = (TextView) findViewById(R.id.tv_rentcar);
tv_costresult = (TextView) findViewById(R.id.tv_result);
btndateFrom = (Button) findViewById(R.id.btn_fromdate);
btndateTo = (Button) findViewById(R.id.btn_todate);
btncalculate = (Button) findViewById(R.id.btn_result);
tv_costresult.setVisibility(View.INVISIBLE);
Calendar calendar1 = Calendar.getInstance();
CurrentYear1 = calendar1.get(Calendar.YEAR);
CurrentMonth1 = calendar1.get(Calendar.MONTH);
CurrentDay1 = calendar1.get(Calendar.DAY_OF_MONTH);
Calendar calendar2 = Calendar.getInstance();
CurrentYear2 = calendar2.get(Calendar.YEAR);
CurrentMonth2 = calendar2.get(Calendar.MONTH);
CurrentDay2 = calendar2.get(Calendar.DAY_OF_MONTH);
showDialogOnButtonClick();
calendar1.set(CurrentYear1, CurrentMonth1, CurrentDay1);
calendar2.set(CurrentYear2, CurrentMonth2, CurrentDay2);
n = (calendar2.getTimeInMillis() - calendar1.getTimeInMillis() );
totalcost = n * rent;
btncalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
totalcost = n * rent;
tv_costresult.setText("The Cost of car from " +CurrentMonth1+"/"+CurrentDay1+"/"+CurrentYear1+" to "+CurrentMonth1+"/"+CurrentDay1+"/"+CurrentYear1+" is "+totalcost);
tv_costresult.setVisibility(View.VISIBLE);
}
});
}
public void showDialogOnButtonClick()
{
btndateFrom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(dialog_id1);
}
});
btndateTo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(dialog_id2);
}
});
}
@Override
protected Dialog onCreateDialog(int id)
{
if(id == dialog_id1)
return new DatePickerDialog(MainActivity.this, setDateListener1, CurrentYear1, CurrentMonth1, CurrentDay1);
if(id == dialog_id2)
return new DatePickerDialog(MainActivity.this, setDateListener2, CurrentYear2, CurrentMonth2, CurrentDay2);
else
return null;
}
private DatePickerDialog.OnDateSetListener setDateListener1 = new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
CurrentYear1 = year;
CurrentMonth1 = month + 1;
CurrentDay1 = dayOfMonth;
Toast.makeText(MainActivity.this, "From Date is " + CurrentMonth1 +"/" + CurrentDay1+ "/" + CurrentYear1, Toast.LENGTH_LONG ).show();
}
};
private DatePickerDialog.OnDateSetListener setDateListener2 = new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
CurrentYear2 = year;
CurrentMonth2 = month +1;
CurrentDay2 = dayOfMonth;
Toast.makeText(MainActivity.this, "From Date is " + CurrentMonth2 +"/" + CurrentDay2+ "/" + CurrentYear2, Toast.LENGTH_LONG ).show();
}
};
}
答案 0 :(得分:0)
结果显示为零,因为您在calendar1
onclicklistener之外设置了calendar2
和btncalculate
。 Moverover,在将它们设置为日历之前,您需要递减CurrentMonth1
和CurrentMonth2
。
将此部分代码从onCreate
移至btncalculate.setOnClickListener(...)
CurrentMonth1 = CurrentMonth1 - 1;
CurrentMonth2 = CurrentMonth2 - 1;
calendar1.set(CurrentYear1, CurrentMonth1, CurrentDay1);
calendar2.set(CurrentYear2, CurrentMonth2, CurrentDay2);
n = (calendar2.getTimeInMillis() - calendar1.getTimeInMillis() );
n = n / ( 1000 * 60 * 60 * 24 );
totalcost = n * rent;