From b134042a75222e40cb7cd844d0de1fd208502cbc Mon Sep 17 00:00:00 2001 From: Frank Montalvo Ochoa Date: Mon, 22 Mar 2021 15:22:17 -0500 Subject: [PATCH] Ahora la interfaz de usuario se adapta mejor a los dispositivos. Se refactoriza el codigo. Signed-off-by: Frank Montalvo Ochoa --- app/build.gradle | 6 +- .../fmontalvoo/converter/MainActivity.java | 30 +- .../controller/ConverterController.java | 10 +- app/src/main/res/drawable/backspace.xml | 4 + app/src/main/res/layout-v21/activity_main.xml | 681 ++++++++++-------- app/src/main/res/layout/activity_main.xml | 679 +++++++++-------- app/src/main/res/layout/spinner_item.xml | 7 + 7 files changed, 766 insertions(+), 651 deletions(-) create mode 100644 app/src/main/res/drawable/backspace.xml create mode 100644 app/src/main/res/layout/spinner_item.xml diff --git a/app/build.gradle b/app/build.gradle index 35178b9..913cd75 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,10 +8,10 @@ android { defaultConfig { applicationId "com.fmontalvoo.converter" - minSdkVersion 17 + minSdkVersion 16 targetSdkVersion 30 - versionCode 3 - versionName "1.3" + versionCode 4 + versionName "1.4" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/java/com/fmontalvoo/converter/MainActivity.java b/app/src/main/java/com/fmontalvoo/converter/MainActivity.java index 61b4251..dfb17ed 100644 --- a/app/src/main/java/com/fmontalvoo/converter/MainActivity.java +++ b/app/src/main/java/com/fmontalvoo/converter/MainActivity.java @@ -8,6 +8,7 @@ import android.os.Bundle; import android.text.Editable; import android.text.InputType; +import android.text.SpannableStringBuilder; import android.text.method.ScrollingMovementMethod; import android.view.Gravity; import android.view.View; @@ -21,15 +22,13 @@ import com.fmontalvoo.converter.controller.ConverterController; -public class MainActivity extends AppCompatActivity implements View.OnClickListener { +public class MainActivity extends AppCompatActivity { private EditText txtNumber; private Spinner spnBase; private Spinner spnGoal; private TextView txtResult; private Button[] numbers; - private Button btnClear; - private Button btnDelete; private ConverterController converterController; @@ -49,7 +48,7 @@ protected void onCreate(Bundle savedInstanceState) { spnBase = findViewById(R.id.spnBase); String[] spnBaseValues = {getString(R.string.binary), getString(R.string.octal), getString(R.string.decimal), getString(R.string.hexadecimal)}; - spnBase.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, spnBaseValues)); + spnBase.setAdapter(new ArrayAdapter(this, R.layout.spinner_item, spnBaseValues)); spnBase = findViewById(R.id.spnBase); spnBase.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override @@ -57,8 +56,7 @@ public void onItemSelected(AdapterView parent, View view, int position, long int a = optionA, b = -1; String txt = txtNumber.getText().toString(); optionA = spnBase.getSelectedItemPosition(); - view.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); - ((TextView) view).setGravity(Gravity.CENTER); + switch (optionA) { case 0: binaryConfig(); @@ -78,7 +76,7 @@ public void onItemSelected(AdapterView parent, View view, int position, long break; } txtNumber.setText(""); - txtNumber.append(converterController.convert(format(txt), options(a, b))); + txtNumber.append(format(converterController.convert(txt, options(a, b)))); } @Override @@ -90,12 +88,10 @@ public void onNothingSelected(AdapterView parent) { spnGoal = findViewById(R.id.spnGoal); String[] spnGoalValues = {getString(R.string.hexadecimal), getString(R.string.decimal), getString(R.string.octal), getString(R.string.binary)}; - spnGoal.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, spnGoalValues)); + spnGoal.setAdapter(new ArrayAdapter<>(this, R.layout.spinner_item, spnGoalValues)); spnGoal.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { - view.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); - ((TextView) view).setGravity(Gravity.CENTER); optionB = spnGoal.getSelectedItemPosition(); convert(); } @@ -134,18 +130,10 @@ public void onNothingSelected(AdapterView parent) { numbers[14] = findViewById(R.id.btnE); numbers[15] = findViewById(R.id.btnF); - for (Button number : numbers) number.setOnClickListener(this); - - btnClear = findViewById(R.id.btnClear); - btnClear.setOnClickListener(this); - - btnDelete = findViewById(R.id.btnDelete); - btnDelete.setOnClickListener(this); - } - @Override - public void onClick(View v) { + + public void onPressed(View v) { int len = txtNumber.length(); int id = v.getId(); if (id == R.id.btnZero) { @@ -185,7 +173,7 @@ public void onClick(View v) { txtResult.setText(""); } else if (id == R.id.btnDelete) { if (len != 0) { - String txt = txtNumber.getText().toString().substring(0, len - 1); + String txt = txtNumber.getText().toString().substring(0, len - 1).trim(); txtNumber.setText(""); txtNumber.append(txt); } diff --git a/app/src/main/java/com/fmontalvoo/converter/controller/ConverterController.java b/app/src/main/java/com/fmontalvoo/converter/controller/ConverterController.java index 8d2a9db..f60cb09 100644 --- a/app/src/main/java/com/fmontalvoo/converter/controller/ConverterController.java +++ b/app/src/main/java/com/fmontalvoo/converter/controller/ConverterController.java @@ -9,7 +9,7 @@ public class ConverterController { public String convert(String number, int option) { if (number == null || number.isEmpty()) return ""; - number = number.replace(" ",""); + number = number.replace(" ", ""); switch (option) { case 1: return binaryToHexadecimal(number); @@ -163,16 +163,16 @@ private String hexadecimalToDecimal(String hexadecimal) { BigInteger decimal = BigInteger.ZERO; final String characters = "ABCDEF"; BigInteger len = new BigInteger(String.valueOf(hexadecimal.length())); - BigInteger[] oremun = new BigInteger[len.intValue()]; + BigInteger[] number = new BigInteger[len.intValue()]; for (BigInteger i = BigInteger.ZERO; i.compareTo(len) < 0; i = i.add(BigInteger.ONE)) { for (BigInteger j = BigInteger.ZERO; j.compareTo(BigInteger.valueOf(characters.length())) < 0; j = j .add(BigInteger.ONE)) { char c = hexadecimal.charAt(i.intValue()); if (c == characters.charAt(j.intValue())) { - oremun[i.intValue()] = numbers(c); + number[i.intValue()] = numbers(c); } else if (c < 65) { - oremun[i.intValue()] = new BigInteger("" + hexadecimal.charAt(i.intValue())); + number[i.intValue()] = new BigInteger("" + hexadecimal.charAt(i.intValue())); } } @@ -180,7 +180,7 @@ private String hexadecimalToDecimal(String hexadecimal) { for (BigInteger i = len.subtract(BigInteger.ONE), j = BigInteger.ZERO; i.compareTo(BigInteger.ZERO) >= 0; i = i .subtract(BigInteger.ONE), j = j.add(BigInteger.ONE)) { - decimal = decimal.add(oremun[i.intValue()].multiply(powerOf(SIXTEEN, j))); + decimal = decimal.add(number[i.intValue()].multiply(powerOf(SIXTEEN, j))); } return decimal.toString(); } diff --git a/app/src/main/res/drawable/backspace.xml b/app/src/main/res/drawable/backspace.xml new file mode 100644 index 0000000..163c439 --- /dev/null +++ b/app/src/main/res/drawable/backspace.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/layout-v21/activity_main.xml b/app/src/main/res/layout-v21/activity_main.xml index 90fb9f9..f8abf34 100644 --- a/app/src/main/res/layout-v21/activity_main.xml +++ b/app/src/main/res/layout-v21/activity_main.xml @@ -7,343 +7,400 @@ android:background="#FFFFFF" tools:context=".MainActivity"> - - - - - + + + + android:layout_height="wrap_content" + android:layout_weight="1" /> - + android:textSize="18sp" + tools:ignore="TextFields" /> - - - - - - + android:layout_height="wrap_content" + android:layout_weight="1" /> + android:layout_height="wrap_content" + android:layout_weight="1"> -