gpt4 book ai didi

android - 我需要根据事件日期显示和隐藏复选框

转载 作者:行者123 更新时间:2023-11-30 01:53:48 25 4
gpt4 key购买 nike

我需要根据事件日期显示和隐藏复选框....我在事件日、2 天前、一周前在我的代码中调用了 3 个复选框....在添加事件中,添加时日期本身,它应该计算间隔。根据当前日期和事件日期之间的间隔,我需要更改复选框的可见性。例如,如果间隔为 2 天......我只需要显示 Activity 日和复选框前 2 天....为此我需要计算间隔。我需要为复选框添加代码....请帮我找出代码...

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_occasion);

context = getApplicationContext();

seekBar_startTime = (SeekBar) findViewById(R.id.seekBar1);
seekBar_startTime.setProgress(0);
seekBar_startTime.setMax(1440); //minutes in 24 hours(24*60)
seekBar_startTime.setProgress(1);
textView_startTime = (TextView) findViewById(R.id.textView1);

seekBar_endTime = (SeekBar) findViewById(R.id.seekBar2);
seekBar_endTime.setProgress(0);
seekBar_endTime.setMax(1440); //minutes in 24 hours(24*60)
seekBar_endTime.setProgress(1);
textView_endTime = (TextView) findViewById(R.id.textView2);

btn_Date = (Button) findViewById(R.id.event_date_button);
event_Place = (TextView) findViewById(R.id.Enter_Place);
custom_Text = (TextView) findViewById(R.id.Custom_Text);
checkBox_onEventDay=(CheckBox) findViewById(R.id.cb_onEventDay);
checkBox_2DaysBefore=(CheckBox) findViewById(R.id.cb_2DaysBefore);
checkBox_aWeekBefore=(CheckBox) findViewById(R.id.cb_aWeekBefore);

btn_create_button = (Button) findViewById(R.id.create_button);
sharedpreferences = context.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
dbHelper = new DatabaseHelper(context);



btn_Date.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

DatePickerFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "Occasion Date");
//Calendar eventDay = Calendar.getInstance();
try {
String eventDate = btn_Date.getText().toString();
DateFormat date = new SimpleDateFormat("dd-M-yyyy");
Date date1 = date.parse(eventDate);
DateFormat convertDate = new SimpleDateFormat(" yyyy-MM-dd hh:mm:ss");
eventDate = convertDate.format(date1);
eventMO.setEventDate(eventDate);



Date currentDate = new Date();

long diff = date1.getTime()-currentDate.getTime();
long seconds = diff / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long daysBefore = hours / 24;
hours%=24;
} catch (ParseException e) {

e.printStackTrace();
}
if (daysBefore >= 7) {
if ((daysBefore == 7 && hours > 9) != true) {
checkBox_aWeekBefore.setVisibility(View.VISIBLE);
}
checkBox_2DaysBefore.setVisibility(View.VISIBLE);

checkBox_onEventDay.setVisibility(View.VISIBLE);
}
else if (daysBefore >= 2) {
if ((daysBefore == 2 && hours > 9) != true) {
checkBox_aWeekBefore.setVisibility(View.INVISIBLE);
//checkBox_2DaysBefore.setVisibility(View.VISIBLE);
}
//checkBox_onEventDay.setVisibility(View.VISIBLE);
}
else if (daysBefore >= 1) {
checkBox_aWeekBefore.setVisibility(View.INVISIBLE);
checkBox_2DaysBefore.setVisibility(View.INVISIBLE);
//checkBox_onEventDay.setVisibility(View.VISIBLE);
}
}


});
/eventid get from OccasionFragment here
Bundle bundle = this.getIntent().getExtras();
if (bundle != null) {

Long eventId1 = bundle.getLong("EventID");
eventId2 = eventId1;
String eventName = bundle.getString("EventName");
custom_Text.setText(eventName);
String eventPlace = bundle.getString("EventPlace");
event_Place.setText(eventPlace);
String eventDate = bundle.getString("EventDate");
try {
//String eventDate = bundle.getString("EventDate");
//time string removed from date here
String result = eventDate.split(" ")[0];
DateFormat sourceDate = new SimpleDateFormat("yyyy-MM-dd");
// parse the date string into Date object
Date date = sourceDate.parse(result);
DateFormat convertDate = new SimpleDateFormat("dd-M-yyyy");
// format the date into another format
result = convertDate.format(date);
btn_Date.setText(result);


} catch (ParseException e) {

e.printStackTrace();
}
//Event Start Time and Event End Time string added before the time here
String eventStart = bundle.getString("EventStart");
textView_startTime.setText("Event Start Time :" + eventStart);
String eventEnd = bundle.getString("EventEnd");
textView_endTime.setText("Event End Time :" + eventEnd);

}

btn_create_button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

doOnCLickAndOnOptionSelected();

}
});


}

@SuppressLint("ValidFragment")
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {
Date = new StringBuilder().append(year).append("-").append(month + 1).append("-").append(day).append(" ").toString();
btn_Date.setText(new StringBuilder().append(day).append("-").append(month + 1).append("-").append(year).append(" ").toString());
}
}

}

我在点击监听器的 btn_date 中尝试了这段代码来计算添加日期时的间隔......但它不起作用......我需要在添加日期本身时可见和不可见..请帮助我..

添加事件包括事件名称、地点、日期、开始时间、结束时间...在此之后我只有 3 个复选框..然后保存按钮以保存事件

最佳答案

删除 hours, daysBefore... 的声明像这样使用...这就是问题所在

  hours = minutes / 60;
daysBefore = hours / 24;

你计算错了,求剩余天数。

diff = event_date - current_date

long diff = date1.getTime() - currentDate.getTime() ;



try {
String eventDate = btn_Date.getText().toString();
DateFormat date = new SimpleDateFormat("dd-M-yyyy");
Date date1 = date.parse(eventDate);
DateFormat convertDate = new SimpleDateFormat(" yyyy-MM-dd hh:mm:ss");
eventDate = convertDate.format(date1);
eventMO.setEventDate(eventDate);



Date currentDate = new Date();
//This is remainder kind of app right
//long diff = currentDate.getTime() - date1.getTime();

//days_remaining - current
long diff = date1.getTime() - currentDate.getTime() ;
long seconds = diff / 1000;
long minutes = seconds / 60;
hours = minutes / 60;
daysBefore = hours / 24;

//you need get remaining hours
hours %= 24;

if (daysBefore >= 7) {
if ((daysBefore == 7 && hours > 9) != true) {
checkBox_aWeekBefore.setVisibility(View.VISIBLE);
}
checkBox_2DaysBefore.setVisibility(View.VISIBLE);

checkBox_onEventDay.setVisibility(View.VISIBLE);
}
else if (daysBefore >= 2) {
if ((daysBefore == 2 && hours > 9) != true) {
checkBox_aWeekBefore.setVisibility(View.INVISIBLE);
//checkBox_2DaysBefore.setVisibility(View.VISIBLE);
}
//checkBox_onEventDay.setVisibility(View.VISIBLE);
}
else if (daysBefore >= 1) {
checkBox_aWeekBefore.setVisibility(View.INVISIBLE);
checkBox_2DaysBefore.setVisibility(View.INVISIBLE);
//checkBox_onEventDay.setVisibility(View.VISIBLE);
}
} catch (ParseException e) {

e.printStackTrace();
}

关于android - 我需要根据事件日期显示和隐藏复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32583086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com