我想在同一个活动中实现两个波斯语DateTimePickers,但是两个名为“onTimeSet”的方法& “onDateSet定义一次......
我如何在每个TimePickers和DatePickers的一个活动中两次使用这些方法?
另外我使用这个库:https://github.com/mohamad-amin/PersianMaterialDateTimePicker
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.mohamadamin.persianmaterialdatetimepicker.date.DatePickerDialog;
import com.mohamadamin.persianmaterialdatetimepicker.time.RadialPickerLayout;
import com.mohamadamin.persianmaterialdatetimepicker.time.TimePickerDialog;
import com.mohamadamin.persianmaterialdatetimepicker.utils.PersianCalendar;
public class TimeActivity extends AppCompatActivity implements
TimePickerDialog.OnTimeSetListener,
DatePickerDialog.OnDateSetListener,
View.OnClickListener {
//VARIABLE DEFINITION
Button btn_start_time = (Button) findViewById(R.id.start_time_button);
Button btn_start_date = (Button) findViewById(R.id.start_date_button);
TextView txt_start_time = (TextView) findViewById(R.id.start_time_textview);
TextView txt_start_date = (TextView) findViewById(R.id.start_date_textview);
PersianCalendar persianCalendar1 = new PersianCalendar();
PersianCalendar persianCalendar2 = new PersianCalendar();
private boolean mode24Hours;
TimePickerDialog timePickerDialog1;
DatePickerDialog datePickerDialog1;
TimePickerDialog timePickerDialog2;
DatePickerDialog datePickerDialog2;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_time_button: {
timePickerDialog1 = TimePickerDialog.newInstance(
TimeActivity.this,
persianCalendar1.get(PersianCalendar.HOUR_OF_DAY),
persianCalendar1.get(PersianCalendar.MINUTE),
mode24Hours
);
timePickerDialog1.setThemeDark(false);
timePickerDialog1.show(getFragmentManager(), "TimePickerDialog");
/*timePickerDialog1.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
Log.d( "TimePickerDialog", "Dialog was cancelled");
}
});*/
break;
}
case R.id.start_date_button: {
datePickerDialog1 = DatePickerDialog.newInstance(
TimeActivity.this,
persianCalendar1.getPersianYear(),
persianCalendar1.getPersianMonth(),
persianCalendar1.getPersianDay()
);
datePickerDialog1.setThemeDark(false);
datePickerDialog1.show(getFragmentManager(), "TimePickerDialog");
break;
}
case R.id.end_time_button: {
timePickerDialog2 = TimePickerDialog.newInstance(
TimeActivity.this,
persianCalendar2.get(PersianCalendar.HOUR_OF_DAY),
persianCalendar2.get(PersianCalendar.MINUTE),
mode24Hours
);
timePickerDialog2.setThemeDark(false);
timePickerDialog2.show(getFragmentManager(), "TimePickerDialog");
break;
}
case R.id.end_date_button: {
datePickerDialog2 = DatePickerDialog.newInstance(
TimeActivity.this,
persianCalendar2.getPersianYear(),
persianCalendar2.getPersianMonth(),
persianCalendar2.getPersianDay()
);
datePickerDialog2.setThemeDark(false);
datePickerDialog2.show(getFragmentManager(), "TimePickerDialog");
break;
}
default:
break;
}
}
@Override
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
String time = "You picked the following time: " + hourOfDay + "h" + minute;
txt_start_time.setText(time);
}
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
String date = "You picked the following date: " + dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;
txt_start_date.setText(date);
}
}
答案 0 :(得分:0)
你需要有两个听众
DatePickerDialog.OnDateSetListener one_dateListener,two_dateListener;
监听器实现如下:
one_dateListener= new OnDateSetListener(){
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
...
}
}
};
two_dateListener= new OnDateSetListener(){
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
.....
}
};
在对话框中使用这些侦听器:
int DATE_PICKER_TO = 0;
int DATE_PICKER_FROM = 1;
@Override
protected Dialog onCreateDialog(int id) {
switch(id){
case DATE_PICKER_ONE:
return new DatePickerDialog(this, one_dateListener, from_year, from_month, from_day);
case DATE_PICKER_TWO:
return new DatePickerDialog(this, two_dateListener, to_year, to_month, to_day);
}
return null;
}