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;
import java.util.Random;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public class MainActivity extends AppCompatActivity {
final private String COUNTER = "counter";
......@@ -40,32 +40,26 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
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 onClick(View view) {
switch(view.getId()) {
case R.id.increment:
setCounter(counter+1);
break;
case R.id.decrement:
setCounter(counter-1);
break;
case R.id.reset:
setCounter(0);
break;
case R.id.random:
setCounter(rng.nextInt(1000));
break;
}
public void clickReset(View v) {
setCounter(0);
}
public void clickRandom(View v) {
setCounter(rng.nextInt(1000)-500);
}
private void setCounter(int newValue) {
counter = newValue;
textView.setText(Integer.toString(counter));
decrButton.setEnabled(counter > 0);
//decrButton.setEnabled(counter > 0);
resetButton.setEnabled(counter != 0);
}
}
......@@ -35,7 +35,7 @@
android:id="@+id/decrement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:onClick="clickDecrement"
android:text="@string/decrement"
app:layout_constraintEnd_toStartOf="@+id/increment"
app:layout_constraintStart_toStartOf="parent"
......@@ -45,7 +45,7 @@
android:id="@+id/increment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:onClick="clickIncrement"
android:text="@string/increment"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
......@@ -57,7 +57,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:onClick="clickReset"
android:text="@string/reset" />
<Button
......@@ -65,7 +65,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:onClick="clickRandom"
android:text="@string/random" />
</LinearLayout>
......
Markdown is supported
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