From 13e7a5f9fe25b64b348ce7e0dae340abda93d5f4 Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 10 Apr 2016 23:26:55 +0200 Subject: [PATCH] Add E button to EditVariableFragment --- .../android/calculator/EditorView.java | 9 ------ .../variables/EditVariableFragment.java | 31 +++++++++++++++++- .../calculator/view/EditTextCompat.java | 9 ++---- .../res/layout/fragment_variable_edit.xml | 32 +++++++++++++++---- .../main/res/values/text_non_translatable.xml | 2 ++ app/src/main/res/values/text_strings.xml | 1 - 6 files changed, 59 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/EditorView.java b/app/src/main/java/org/solovyev/android/calculator/EditorView.java index 685eabef..2f928218 100644 --- a/app/src/main/java/org/solovyev/android/calculator/EditorView.java +++ b/app/src/main/java/org/solovyev/android/calculator/EditorView.java @@ -22,14 +22,11 @@ package org.solovyev.android.calculator; -import android.annotation.TargetApi; import android.content.Context; -import android.os.Build; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.ContextMenu; - import org.solovyev.android.Check; import org.solovyev.android.calculator.view.EditTextCompat; import org.solovyev.android.views.Adjuster; @@ -58,12 +55,6 @@ public class EditorView extends EditTextCompat { init(); } - @TargetApi(Build.VERSION_CODES.LOLLIPOP) - public EditorView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { - super(context, attrs, defStyleAttr, defStyleRes); - init(); - } - private void init() { if (!App.isFloatingCalculator(getContext())) { Adjuster.adjustText(this, 0.22f, diff --git a/app/src/main/java/org/solovyev/android/calculator/variables/EditVariableFragment.java b/app/src/main/java/org/solovyev/android/calculator/variables/EditVariableFragment.java index 79c4e738..a7ac1257 100644 --- a/app/src/main/java/org/solovyev/android/calculator/variables/EditVariableFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/variables/EditVariableFragment.java @@ -34,6 +34,9 @@ import android.support.design.widget.TextInputLayout; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v7.app.AlertDialog; +import android.text.Editable; +import android.text.InputFilter; +import android.text.SpannableStringBuilder; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; @@ -45,10 +48,11 @@ import android.widget.PopupWindow; import butterknife.Bind; import butterknife.ButterKnife; import dagger.Lazy; +import jscl.JsclMathEngine; import jscl.math.function.IConstant; +import midpcalc.Real; import org.solovyev.android.Check; import org.solovyev.android.calculator.*; -import org.solovyev.android.calculator.RemovalConfirmationDialog; import org.solovyev.android.calculator.functions.FunctionsRegistry; import org.solovyev.android.calculator.keyboard.FloatingKeyboard; import org.solovyev.android.calculator.keyboard.FloatingKeyboardWindow; @@ -87,6 +91,8 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF TextInputLayout valueLabel; @Bind(R.id.variable_value) EditText valueView; + @Bind(R.id.variable_exponent_button) + Button exponentButton; @Bind(R.id.variable_description) EditText descriptionView; @Inject @@ -304,6 +310,13 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF nameView.setOnFocusChangeListener(this); nameView.setOnKeyListener(this); valueView.setOnFocusChangeListener(this); + valueView.setEditableFactory(new Editable.Factory() { + @Override + public Editable newEditable(CharSequence source) { + return new NoFiltersEditable(source); + } + }); + exponentButton.setOnClickListener(this); descriptionView.setOnFocusChangeListener(this); keyboardButton.setOnClickListener(this); @@ -352,6 +365,11 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF showKeyboard(); } break; + case R.id.variable_exponent_button: + final int start = Math.max(valueView.getSelectionStart(), 0); + final int end = Math.max(valueView.getSelectionEnd(), 0); + valueView.getText().replace(Math.min(start, end), Math.max(start, end), "E", 0, 1); + break; default: super.onClick(v); break; @@ -379,6 +397,17 @@ public class EditVariableFragment extends BaseDialogFragment implements View.OnF keyboardWindow.show(new GreekFloatingKeyboard(keyboardUser), getDialog()); } + private static class NoFiltersEditable extends SpannableStringBuilder { + public NoFiltersEditable(CharSequence source) { + super(source); + } + + @Override + public void setFilters(InputFilter[] filters) { + // we don't want filters as we want to support numbers in scientific notation + } + } + private class KeyboardUser implements FloatingKeyboard.User { @NonNull @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/view/EditTextCompat.java b/app/src/main/java/org/solovyev/android/calculator/view/EditTextCompat.java index 79f393b6..81a52e2a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/view/EditTextCompat.java +++ b/app/src/main/java/org/solovyev/android/calculator/view/EditTextCompat.java @@ -1,8 +1,8 @@ package org.solovyev.android.calculator.view; -import android.annotation.TargetApi; import android.content.Context; import android.os.Build; +import android.support.design.widget.TextInputEditText; import android.text.InputType; import android.util.AttributeSet; import android.util.Log; @@ -12,7 +12,7 @@ import org.solovyev.android.Check; import javax.annotation.Nullable; import java.lang.reflect.Method; -public class EditTextCompat extends EditText { +public class EditTextCompat extends TextInputEditText { @Nullable private static Method setShowSoftInputOnFocusMethod; @@ -30,11 +30,6 @@ public class EditTextCompat extends EditText { super(context, attrs, defStyleAttr); } - @TargetApi(Build.VERSION_CODES.LOLLIPOP) - public EditTextCompat(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { - super(context, attrs, defStyleAttr, defStyleRes); - } - public void dontShowSoftInputOnFocusCompat() { setShowSoftInputOnFocusCompat(false); } diff --git a/app/src/main/res/layout/fragment_variable_edit.xml b/app/src/main/res/layout/fragment_variable_edit.xml index 7c39891e..26b9ce45 100644 --- a/app/src/main/res/layout/fragment_variable_edit.xml +++ b/app/src/main/res/layout/fragment_variable_edit.xml @@ -65,18 +65,36 @@ tools:ignore="UnusedAttribute" /> - - + + + + +