Java 当两个日期选择器的日期相同时,如何弹出单选按钮?
我正在开发一个简单的应用程序,其中我使用了两个日期选择器,它们减去两个日期并显示结果。但我想,如果这两个日期与2017年4月6日相同,那么一旦计算出这两个日期相同,就会弹出两个单选按钮来选择全天和半天 怎么做Java 当两个日期选择器的日期相同时,如何弹出单选按钮?,java,android,datepicker,android-radiobutton,Java,Android,Datepicker,Android Radiobutton,我正在开发一个简单的应用程序,其中我使用了两个日期选择器,它们减去两个日期并显示结果。但我想,如果这两个日期与2017年4月6日相同,那么一旦计算出这两个日期相同,就会弹出两个单选按钮来选择全天和半天 怎么做 public class Leave extends AppCompatActivity { TextView date; private DatePickerDialog datePickerDialog; TextView date2; //TextView setDay;
public class Leave extends AppCompatActivity {
TextView date;
private DatePickerDialog datePickerDialog;
TextView date2;
//TextView setDay;
private DatePickerDialog datePickerDialog2;
TextView no_of_days;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leave);
date = (TextView) findViewById(R.id.date);
date2 = (TextView) findViewById(R.id.date2);
//setDay = (TextView) findViewById(R.id.setDay);
no_of_days = (TextView) findViewById(R.id.no_of_days);
// initiate the date picker and a button
date = (TextView) findViewById(R.id.date);
// perform click event on edit text
date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calender class's instance and get current date , month and year from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog = new DatePickerDialog(Leave.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
date.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
});
// initiate the date picker and a button
date2 = (TextView) findViewById(R.id.date2);
// perform click event on edit text
date2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calender class's instance and get current date , month and year from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog2 = new DatePickerDialog(Leave.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
date2.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog2.show();
}
});
no_of_days.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
String date01 = date.getText().toString();
String date02 = date2.getText().toString();
try {
Date d = format.parse(date01);
Date d1 = format.parse(date02);
getDifferenceDays(d, d1);
} catch (ParseException e) {
e.printStackTrace();
}
}
});
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.leave_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
public class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
SpinnerActivity() throws ParseException {
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
}
}
private void setupRadios() {
RadioButton radio_full = (RadioButton) findViewById(R.id.radio_full);
RadioButton radio_Half = (RadioButton) findViewById(R.id.radio_Half);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioleave);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
int buttonId = radioGroup.getCheckedRadioButtonId();
switch (buttonId) {
case R.id.radio_full:
Toast.makeText(getApplicationContext(), "You have selected Full Day Leave", Toast.LENGTH_SHORT).show();
break;
case R.id.radio_Half:
Toast.makeText(getApplicationContext(), "You have selected Half Day Leave", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
public void getDifferenceDays(Date d1, Date d2) {
int daysdiff = 0;
long diff = d2.getTime() - d1.getTime();
long diffDays = diff / (24 * 60 * 60 * 1000) + 1;
daysdiff = (int) diffDays;
no_of_days.setText(Integer.toString(daysdiff));
System.out.println("day count=>" + daysdiff);
}
公共课休假活动{
文本查看日期;
专用日期选择器Dialog日期选择器Dialog;
文本视图日期2;
//文本视图设置日;
专用日期选择器Dialog DatePickerDialog 2;
TextView无天数;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_-leave);
日期=(TextView)findViewById(R.id.date);
date2=(TextView)findViewById(R.id.date2);
//setDay=(TextView)findViewById(R.id.setDay);
无天数=(TextView)findViewById(R.id.no\u of\u days);
//启动日期选择器和按钮
日期=(TextView)findViewById(R.id.date);
//对编辑文本执行单击事件
date.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//Calendar类的实例,并从Calendar获取当前日期、月份和年份
最终日历c=Calendar.getInstance();
int mYear=c.get(Calendar.YEAR);//当前年份
int mMonth=c.get(Calendar.MONTH);//当前月份
int mDay=c.get(Calendar.DAY/u/u MONTH);//当前日期
//日期选择器对话框
datePickerDialog=新的datePickerDialog(离开.this,
新建DatePickerDialog.OnDateSetListener(){
@凌驾
公共无效onDateSet(日期选择器视图,整数年,
int monthOfYear,int dayOfMonth){
//在编辑文本中设置月日、月和年值
date.setText(dayOfMonth+“/”
+(年+1)+“/”+年);
}
},mYear,Ammonth,mDay);
datePickerDialog.show();
}
});
//启动日期选择器和按钮
date2=(TextView)findViewById(R.id.date2);
//对编辑文本执行单击事件
date2.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//Calendar类的实例,并从Calendar获取当前日期、月份和年份
最终日历c=Calendar.getInstance();
int mYear=c.get(Calendar.YEAR);//当前年份
int mMonth=c.get(Calendar.MONTH);//当前月份
int mDay=c.get(Calendar.DAY/u/u MONTH);//当前日期
//日期选择器对话框
DatePickerDialog 2=新的DatePickerDialog(离开.this,
新建DatePickerDialog.OnDateSetListener(){
@凌驾
公共无效onDateSet(日期选择器视图,整数年,
int monthOfYear,int dayOfMonth){
//在编辑文本中设置月日、月和年值
date2.setText(dayOfMonth+“/”
+(年+1)+“/”+年);
}
},mYear,Ammonth,mDay);
datePickerDialog2.show();
}
});
没有多少天。setOnClickListener(新视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
SimpleDataFormat格式=新的SimpleDataFormat(“dd/mm/yyyy”);
字符串date01=date.getText().toString();
字符串date02=date2.getText().toString();
试一试{
dated=format.parse(date01);
dated1=format.parse(date02);
获得不同的天数(d、d1);
}捕获(解析异常){
e、 printStackTrace();
}
}
});
微调器微调器=(微调器)findViewById(R.id.Spinner);
//使用字符串数组和默认微调器布局创建ArrayAdapter
ArrayAdapter=ArrayAdapter.createFromResource(此,
R.array.leave_数组,android.R.layout.simple_微调器_项);
//指定显示选项列表时要使用的布局
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
//将适配器应用于微调器
旋转器。设置适配器(适配器);
}
公共类SpinerActivity扩展活动实现AdapterView.OnItemSelectedListener{
spinneActivity()引发ParseException异常{
}
已选择公共视图(AdapterView父视图、视图、,
内部位置,长id){
//已选择一个项目。您可以使用
//父项getItemAtPosition(pos)
}
未选择公共无效(AdapterView父级){
//另一个接口回调
微调器微调器=(微调器)findViewById(R.id.Spinner);
spinner.setOnItemSelectedListener(此);
}
}
专用收音机(){
RadioButton单选按钮已满=(RadioButton)findViewById(R.id.radio\u full);
RadioButton单选对半=(RadioButton)findViewById(R.id.radio对半);
放射组放射组=(放射组)findViewById(R.id.radioleave);
radioGroup.setOnCheckedChangeListener(新的radioGroup.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(放射组放射组,int i){
int buttonId=radioGroup.getCheckedRadioButtonId();
开关(按钮NID){
案例R.id.收音机已满:
Toast.makeText(getApplicationContext(),“您已选择全天休假”,Toast.LENGTH\u SHORT.show();
打破
案例R.id.无线电部分:
Toast.makeText(getApplicationContext
public class Leave extends AppCompatActivity {
TextView date;
private DatePickerDialog datePickerDialog;
TextView date2;
//TextView setDay;
private DatePickerDialog datePickerDialog2;
TextView no_of_days;
String date1 = "", date2 = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leave);
date = (TextView) findViewById(R.id.date);
date2 = (TextView) findViewById(R.id.date2);
//setDay = (TextView) findViewById(R.id.setDay);
no_of_days = (TextView) findViewById(R.id.no_of_days);
RadioButton radio_full = (RadioButton) findViewById(R.id.radio_full);
RadioButton radio_Half = (RadioButton) findViewById(R.id.radio_Half);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioleave);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
int buttonId = radioGroup.getCheckedRadioButtonId();
switch (buttonId) {
case R.id.radio_full:
Toast.makeText(getApplicationContext(), "You have selected Full Day Leave", Toast.LENGTH_SHORT).show();
break;
case R.id.radio_Half:
Toast.makeText(getApplicationContext(), "You have selected Half Day Leave", Toast.LENGTH_SHORT).show();
break;
}
}
});
// initially hide the radio buttons
radio_full.setVisibility(View.GONE);
radio_Half.setVisibility(View.GONE);
// initiate the date picker and a button
date = (TextView) findViewById(R.id.date);
// perform click event on edit text
date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calender class's instance and get current date , month and year from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog = new DatePickerDialog(Leave.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
// assign the value to date1
date1 = dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year;
date.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
// check if date2 has been set and compare it with date1
if(!TextUtils.isEmpty(date2)) {
// get difference
getDifferenceDays(date, date2);
if(date1.equals(date2)) {
// pop up radio button
radio_full.setVisibility(View.VISIBLE);
radio_Half.setVisibility(View.VISIBLE);
} else {
// hide radio buttons
radio_full.setVisibility(View.GONE);
radio_Half.setVisibility(View.GONE);
}
}
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
});
// initiate the date picker and a button
date2 = (TextView) findViewById(R.id.date2);
// perform click event on edit text
date2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calender class's instance and get current date , month and year from calender
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
// date picker dialog
datePickerDialog2 = new DatePickerDialog(Leave.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// set day of month , month and year value in the edit text
// assign the value to date2
date2 = dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year;
date2.setText(dayOfMonth + "/"
+ (monthOfYear + 1) + "/" + year);
// check if date2 has been set and compare it with date1
if(!TextUtils.isEmpty(date1)) {
// get difference
getDifferenceDays(date, date2);
if(date1.equals(date2)) {
// pop up radio button
radio_full.setVisibility(View.VISIBLE);
radio_Half.setVisibility(View.VISIBLE);
} else {
// hide radio buttons
radio_full.setVisibility(View.GONE);
radio_Half.setVisibility(View.GONE);
}
}
}
}, mYear, mMonth, mDay);
datePickerDialog2.show();
}
});
no_of_days.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
String date01 = date.getText().toString();
String date02 = date2.getText().toString();
try {
Date d = format.parse(date01);
Date d1 = format.parse(date02);
getDifferenceDays(d, d1);
} catch (ParseException e) {
e.printStackTrace();
}
}
});
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.leave_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}