Reputation: 95
I've got several EditTexts in one of my Android app activities. The one for picking a date works perfectly fine, the date picker dialog shows up, you choose a date, the date is then set in that EditText, no problem.
The problem is that when I later open another EditText, one in which you're just suppose to provide some text (the one called 'leggtil'), the Date Picker Dialog shows up again! Anyone know what's wrong? I'll show all relevant code here:
private DatePickerDialog velgdato;
private SimpleDateFormat dateFormat;
private EditText dato;
private Calendar mycalendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lag_liste);
dato = (EditText)findViewById(R.id.laglistedato);
final EditText leggtil = (EditText)findViewById(R.id.laglisteadditem);
mycalendar = Calendar.getInstance();
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mycalendar.set(Calendar.YEAR, year);
mycalendar.set(Calendar.MONTH, monthOfYear);
mycalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
oppdater();
}
};
dato.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
new DatePickerDialog(LagListe.this, date, mycalendar.get(Calendar.YEAR), mycalendar.get(Calendar.MONTH), mycalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
leggtil.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (leggtil.getText().toString() != null) {
Toast.makeText(getApplicationContext(), leggtil.getText().toString(), Toast.LENGTH_SHORT).show();
return true;
}
else
return false;
}
});
}
private void oppdater() {
String format = "MM/dd/yy";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
dato.setText(sdf.format(mycalendar.getTime()));
}
Upvotes: 0
Views: 196
Reputation: 978
The issue is that you set the onFocusChangeListener, which is called every time the Views focus changes (both to in-focus and out-of-focus). Try this:
dato.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
new DatePickerDialog(LagListe.this, date, mycalendar.get(Calendar.YEAR), mycalendar.get(Calendar.MONTH), mycalendar.get(Calendar.DAY_OF_MONTH)).show();
}
}
});
Upvotes: 1
Reputation: 132982
On second EditText click Date Picker is showing due to setOnFocusChangeListener
method is called when focus is remove from first EditText.
Add if condition inside onFocusChange
and show Date Picker only when focus is on First EditText like change onFocusChange
method as :
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(v.getId()==R.id.laglistedato){
new DatePickerDialog(LagListe.this, date,
mycalendar.get(Calendar.YEAR),
mycalendar.get(Calendar.MONTH),
mycalendar.get(Calendar.DAY_OF_MONTH)).show();
}else{
// do nothing...
}
}
Upvotes: 0