Commit 54e3ff2f authored by Christopher League's avatar Christopher League
Browse files

Use separate click methods instead of switch

parent 82a72def
...@@ -8,7 +8,7 @@ import android.widget.TextView; ...@@ -8,7 +8,7 @@ import android.widget.TextView;
import java.util.Random; import java.util.Random;
public class MainActivity extends AppCompatActivity implements View.OnClickListener { public class MainActivity extends AppCompatActivity {
final private String COUNTER = "counter"; final private String COUNTER = "counter";
...@@ -40,32 +40,26 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -40,32 +40,26 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
outState.putInt(COUNTER, counter); outState.putInt(COUNTER, counter);
} }
public void anotherClick(View view) { public void clickIncrement(View v) {
setCounter(counter+1);
}
public void clickDecrement(View v) {
setCounter(counter-1);
} }
@Override public void clickReset(View v) {
public void onClick(View view) { setCounter(0);
switch(view.getId()) { }
case R.id.increment:
setCounter(counter+1); public void clickRandom(View v) {
break; setCounter(rng.nextInt(1000)-500);
case R.id.decrement:
setCounter(counter-1);
break;
case R.id.reset:
setCounter(0);
break;
case R.id.random:
setCounter(rng.nextInt(1000));
break;
}
} }
private void setCounter(int newValue) { private void setCounter(int newValue) {
counter = newValue; counter = newValue;
textView.setText(Integer.toString(counter)); textView.setText(Integer.toString(counter));
decrButton.setEnabled(counter > 0); //decrButton.setEnabled(counter > 0);
resetButton.setEnabled(counter != 0); resetButton.setEnabled(counter != 0);
} }
} }
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
android:id="@+id/decrement" android:id="@+id/decrement"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:onClick="onClick" android:onClick="clickDecrement"
android:text="@string/decrement" android:text="@string/decrement"
app:layout_constraintEnd_toStartOf="@+id/increment" app:layout_constraintEnd_toStartOf="@+id/increment"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
android:id="@+id/increment" android:id="@+id/increment"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:onClick="onClick" android:onClick="clickIncrement"
android:text="@string/increment" android:text="@string/increment"
app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
...@@ -57,7 +57,7 @@ ...@@ -57,7 +57,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:onClick="onClick" android:onClick="clickReset"
android:text="@string/reset" /> android:text="@string/reset" />
<Button <Button
...@@ -65,7 +65,7 @@ ...@@ -65,7 +65,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:onClick="onClick" android:onClick="clickRandom"
android:text="@string/random" /> android:text="@string/random" />
</LinearLayout> </LinearLayout>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment