use of eu.davidea.common.SimpleTextWatcher in project FlexibleAdapter by davideas.
the class EditItemDialog method onCreateDialog.
@SuppressLint({ "InflateParams", "HandlerLeak" })
@Override
public AlertDialog onCreateDialog(Bundle savedInstanceState) {
//Pick up bundle parameters
Bundle bundle;
if (savedInstanceState == null) {
bundle = getArguments();
} else {
bundle = savedInstanceState;
}
mTitle = bundle.getString(ARG_TITLE);
mPosition = bundle.getInt(ARG_ITEM_POSITION);
//Inflate custom view
View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_edit_item, null);
final EditText editText = (EditText) dialogView.findViewById(R.id.text_edit_title);
//, R.style.AppTheme_AlertDialog);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog_edit_title).setView(dialogView).setNegativeButton(R.string.CANCEL, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Utils.hideSoftInputFrom(getActivity(), editText);
dialog.dismiss();
}
}).setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getListener().onTitleModified(mPosition, editText.getText().toString().trim());
Utils.hideSoftInputFrom(getActivity(), editText);
dialog.dismiss();
}
});
final AlertDialog editDialog = builder.create();
editDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
updateOkButtonState(editDialog, null);
}
});
if (mTitle != null) {
editText.setText(mTitle);
editText.selectAll();
}
editText.requestFocus();
editText.addTextChangedListener(new SimpleTextWatcher() {
private static final long DELAY = 400L;
private static final int TRIGGER = 1;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == TRIGGER) {
updateOkButtonState(editDialog, editText);
}
}
};
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
updateOkButtonState(editDialog, null);
}
@Override
public void afterTextChanged(Editable s) {
mHandler.removeMessages(TRIGGER);
mHandler.sendEmptyMessageDelayed(TRIGGER, DELAY);
}
});
editDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return editDialog;
}
Aggregations