Show keyboard when opening TimeDialog. fixes #436

This commit is contained in:
daniel oeh 2014-06-15 20:35:50 +02:00
parent 5ede220e63
commit 8a951d0dbf
2 changed files with 100 additions and 90 deletions

View File

@ -20,7 +20,7 @@
android:inputType="number" android:inputType="number"
android:maxLength="2" > android:maxLength="2" >
<requestFocus />
</EditText> </EditText>
<Spinner <Spinner

View File

@ -8,6 +8,7 @@ import android.text.TextWatcher;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.view.Window; import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.*; import android.widget.*;
import de.danoeh.antennapod.BuildConfig; import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
@ -15,114 +16,123 @@ import de.danoeh.antennapod.R;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public abstract class TimeDialog extends Dialog { public abstract class TimeDialog extends Dialog {
private static final String TAG = "TimeDialog"; private static final String TAG = "TimeDialog";
private static final int DEFAULT_SPINNER_POSITION = 1; private static final int DEFAULT_SPINNER_POSITION = 1;
private Context context; private Context context;
private EditText etxtTime; private EditText etxtTime;
private Spinner spTimeUnit; private Spinner spTimeUnit;
private Button butConfirm; private Button butConfirm;
private Button butCancel; private Button butCancel;
private TimeUnit[] units = { TimeUnit.SECONDS, TimeUnit.MINUTES, private TimeUnit[] units = {TimeUnit.SECONDS, TimeUnit.MINUTES,
TimeUnit.HOURS }; TimeUnit.HOURS};
public TimeDialog(Context context, int titleTextId, int leftButtonTextId) { public TimeDialog(Context context, int titleTextId, int leftButtonTextId) {
super(context); super(context);
this.context = context; this.context = context;
} }
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); requestWindowFeature(Window.FEATURE_NO_TITLE);
String[] spinnerContent = new String[]{context.getString(R.string.time_unit_seconds), String[] spinnerContent = new String[]{context.getString(R.string.time_unit_seconds),
context.getString(R.string.time_unit_minutes), context.getString(R.string.time_unit_minutes),
context.getString(R.string.time_unit_hours)}; context.getString(R.string.time_unit_hours)};
setContentView(R.layout.time_dialog); setContentView(R.layout.time_dialog);
etxtTime = (EditText) findViewById(R.id.etxtTime); etxtTime = (EditText) findViewById(R.id.etxtTime);
spTimeUnit = (Spinner) findViewById(R.id.spTimeUnit); spTimeUnit = (Spinner) findViewById(R.id.spTimeUnit);
butConfirm = (Button) findViewById(R.id.butConfirm); butConfirm = (Button) findViewById(R.id.butConfirm);
butCancel = (Button) findViewById(R.id.butCancel); butCancel = (Button) findViewById(R.id.butCancel);
butConfirm.setText(R.string.set_sleeptimer_label); butConfirm.setText(R.string.set_sleeptimer_label);
butCancel.setText(R.string.cancel_label); butCancel.setText(R.string.cancel_label);
setTitle(R.string.set_sleeptimer_label); setTitle(R.string.set_sleeptimer_label);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>( ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(
this.getContext(), android.R.layout.simple_spinner_item, this.getContext(), android.R.layout.simple_spinner_item,
spinnerContent); spinnerContent);
spinnerAdapter spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spTimeUnit.setAdapter(spinnerAdapter); spTimeUnit.setAdapter(spinnerAdapter);
spTimeUnit.setSelection(DEFAULT_SPINNER_POSITION); spTimeUnit.setSelection(DEFAULT_SPINNER_POSITION);
butCancel.setOnClickListener(new View.OnClickListener() { butCancel.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
dismiss(); dismiss();
} }
}); });
butConfirm.setOnClickListener(new View.OnClickListener() { butConfirm.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
try { try {
long input = readTimeMillis(); long input = readTimeMillis();
onTimeEntered(input); onTimeEntered(input);
dismiss(); dismiss();
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
e.printStackTrace(); e.printStackTrace();
Toast toast = Toast.makeText(context, Toast toast = Toast.makeText(context,
R.string.time_dialog_invalid_input, R.string.time_dialog_invalid_input,
Toast.LENGTH_LONG); Toast.LENGTH_LONG);
toast.show(); toast.show();
} }
} }
}); });
etxtTime.addTextChangedListener(new TextWatcher() { etxtTime.addTextChangedListener(new TextWatcher() {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
checkInputLength(s.length()); checkInputLength(s.length());
} }
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, public void beforeTextChanged(CharSequence s, int start, int count,
int after) { int after) {
} }
@Override @Override
public void onTextChanged(CharSequence s, int start, int before, public void onTextChanged(CharSequence s, int start, int before,
int count) { int count) {
} }
}); });
checkInputLength(etxtTime.getText().length()); checkInputLength(etxtTime.getText().length());
etxtTime.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(etxtTime, InputMethodManager.SHOW_IMPLICIT);
}
}, 100);
}
private void checkInputLength(int length) {
if (length > 0) {
if (BuildConfig.DEBUG)
Log.d(TAG, "Length is larger than 0, enabling confirm button");
butConfirm.setEnabled(true);
} else {
if (BuildConfig.DEBUG)
Log.d(TAG, "Length is smaller than 0, disabling confirm button");
butConfirm.setEnabled(false);
}
}
public abstract void onTimeEntered(long millis); }
private long readTimeMillis() { private void checkInputLength(int length) {
TimeUnit selectedUnit = units[spTimeUnit.getSelectedItemPosition()]; if (length > 0) {
long value = Long.valueOf(etxtTime.getText().toString()); if (BuildConfig.DEBUG)
return selectedUnit.toMillis(value); Log.d(TAG, "Length is larger than 0, enabling confirm button");
} butConfirm.setEnabled(true);
} else {
if (BuildConfig.DEBUG)
Log.d(TAG, "Length is smaller than 0, disabling confirm button");
butConfirm.setEnabled(false);
}
}
public abstract void onTimeEntered(long millis);
private long readTimeMillis() {
TimeUnit selectedUnit = units[spTimeUnit.getSelectedItemPosition()];
long value = Long.valueOf(etxtTime.getText().toString());
return selectedUnit.toMillis(value);
}
} }