我试图在单击textview时使datepickerdialog和timepickerdialog弹出。我有一个问题,每次我启动我的应用程序时,TimePickerDialog会自动弹出,尽管我甚至没有点击文本框。我能以任何方式解决这个问题吗?
public class MainActivity extends AppCompatActivity implements OnClickListener{
EditText e_tarikh, e_masa;
String tarikh, masa;
private DBOperations db;
private EditText fromDateEtxt;
private EditText toTimeEtxt;
private int mHour, mMinute;
private DatePickerDialog fromDatePickerDialog;
private TimePickerDialog toTimePickerDialog;
private SimpleDateFormat dateFormatter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DBOperations(this);
e_tarikh = (EditText) findViewById(R.id.tarikh);
e_masa = (EditText) findViewById(R.id.masa);
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
findViewsById();
setDateTimeField();
}
public void saveData(View view)
{
tarikh = e_tarikh.getText().toString();
masa = e_masa.getText().toString();
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute("add_info", tarikh, masa);
finish();
}
private void findViewsById() {
fromDateEtxt = (EditText) findViewById(R.id.tarikh);
fromDateEtxt.setInputType(InputType.TYPE_NULL);
fromDateEtxt.requestFocus();
toTimeEtxt = (EditText) findViewById(R.id.masa);
toTimeEtxt.setInputType(InputType.TYPE_NULL);
}
private void setDateTimeField() {
fromDateEtxt.setOnClickListener(this);
toTimeEtxt.setOnClickListener(this);
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
Calendar newCalendar = Calendar.getInstance();
fromDatePickerDialog = new DatePickerDialog(this, new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
toTimePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
e_masa.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
toTimePickerDialog.show();
}
@Override
public void onClick(View view) {
if(view == fromDateEtxt) {
fromDatePickerDialog.show();
} else if(view == toTimeEtxt) {
toTimePickerDialog.show();
}
}
答案 0 :(得分:0)
通过调用此onCreate()
方法,您可以在setDateTimeField();
中创建timePicker
从setDateTimeField();
方法中删除TimePicker:
private void setDateTimeField() {
fromDateEtxt.setOnClickListener(this);
toTimeEtxt.setOnClickListener(this);
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
Calendar newCalendar = Calendar.getInstance();
fromDatePickerDialog = new DatePickerDialog(this, new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
//Remove this
/*toTimePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
e_masa.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
toTimePickerDialog.show();*/
}
答案 1 :(得分:0)
请删除此行:
toTimePickerDialog.show();
来自你的
setDateTimeField()
答案 2 :(得分:0)
您需要做的就是从setDateTimeField()
方法中删除以下行,因为在您点击按钮或textView之前,您不希望显示toTimePickerDialog
。现在它位于onCreate方法中,因此每次运行应用程序时都会运行。
toTimePickerDialog.show();