Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Christopher League
FloodGame
Commits
54e3ff2f
Commit
54e3ff2f
authored
Jul 04, 2019
by
Christopher League
Browse files
Use separate click methods instead of switch
parent
82a72def
Changes
2
Show whitespace changes
Inline
Side-by-side
app/src/main/java/edu/liu/floodgame/MainActivity.java
View file @
54e3ff2f
...
...
@@ -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
v
iew
)
{
public
void
clickIncrement
(
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
:
public
void
clickDecrement
(
View
v
)
{
setCounter
(
counter
-
1
);
break
;
case
R
.
id
.
reset
:
}
public
void
clickReset
(
View
v
)
{
setCounter
(
0
);
break
;
case
R
.
id
.
random
:
setCounter
(
rng
.
nextInt
(
1000
));
break
;
}
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
);
}
}
app/src/main/res/layout/activity_main.xml
View file @
54e3ff2f
...
...
@@ -35,7 +35,7 @@
android:id=
"@+id/decrement"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:onClick=
"
onC
lick"
android:onClick=
"
c
lick
Decrement
"
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=
"
onC
lick"
android:onClick=
"
c
lick
Increment
"
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=
"
onC
lick"
android:onClick=
"
c
lick
Reset
"
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=
"
onC
lick"
android:onClick=
"
c
lick
Random
"
android:text=
"@string/random"
/>
</LinearLayout>
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment