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
82a72def
Commit
82a72def
authored
Jul 04, 2019
by
Christopher League
Browse files
Working with reset and rand buttons too
parent
876b8cf7
Changes
3
Show whitespace changes
Inline
Side-by-side
app/src/main/java/edu/liu/floodgame/MainActivity.java
View file @
82a72def
...
...
@@ -6,12 +6,15 @@ import android.view.View;
import
android.widget.Button
;
import
android.widget.TextView
;
import
java.util.Random
;
public
class
MainActivity
extends
AppCompatActivity
implements
View
.
OnClickListener
{
final
private
String
COUNTER
=
"counter"
;
private
Random
rng
=
new
Random
();
private
TextView
textView
;
private
Button
decrButton
;
private
Button
decrButton
,
resetButton
;
private
int
counter
=
0
;
@Override
...
...
@@ -20,6 +23,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
setContentView
(
R
.
layout
.
activity_main
);
this
.
textView
=
findViewById
(
R
.
id
.
textView
);
this
.
decrButton
=
findViewById
(
R
.
id
.
decrement
);
this
.
resetButton
=
findViewById
(
R
.
id
.
reset
);
if
(
savedInstanceState
==
null
)
{
System
.
out
.
println
(
"No existing bundle"
);
setCounter
(
0
);
...
...
@@ -36,6 +40,10 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
outState
.
putInt
(
COUNTER
,
counter
);
}
public
void
anotherClick
(
View
view
)
{
}
@Override
public
void
onClick
(
View
view
)
{
switch
(
view
.
getId
())
{
...
...
@@ -45,12 +53,19 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
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
)
{
counter
=
newValue
;
textView
.
setText
(
getString
(
R
.
string
.
counter_is
,
counter
));
textView
.
setText
(
Integer
.
toString
(
counter
));
decrButton
.
setEnabled
(
counter
>
0
);
resetButton
.
setEnabled
(
counter
!=
0
);
}
}
app/src/main/res/layout/activity_main.xml
View file @
82a72def
...
...
@@ -12,6 +12,8 @@
android:layout_height=
"wrap_content"
android:layout_marginTop=
"8dp"
android:text=
"@string/greeting"
android:textSize=
"54sp"
app:fontFamily=
"sans-serif-black"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintLeft_toLeftOf=
"parent"
app:layout_constraintRight_toRightOf=
"parent"
...
...
@@ -20,17 +22,18 @@
<LinearLayout
android:id=
"@+id/linearLayout"
style=
"?android:attr/buttonBarStyle"
android:layout_width=
"
match_par
ent"
android:layout_width=
"
wrap_cont
ent"
android:layout_height=
"wrap_content"
android:gravity=
"center"
android:orientation=
"horizontal"
app:layout_constraintBottom_toTopOf=
"@+id/textView"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintTop_toTopOf=
"parent"
>
<Button
android:id=
"@+id/decrement"
style=
"?android:attr/borderlessButtonStyle"
android:layout_width=
"105dp"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:onClick=
"onClick"
android:text=
"@string/decrement"
...
...
@@ -43,13 +46,28 @@
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:onClick=
"onClick"
style=
"?android:attr/borderlessButtonStyle"
android:text=
"@string/increment"
app:layout_constraintBottom_toTopOf=
"@+id/textView"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintStart_toEndOf=
"@+id/decrement"
app:layout_constraintTop_toTopOf=
"parent"
/>
<Button
android:id=
"@+id/reset"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:onClick=
"onClick"
android:text=
"@string/reset"
/>
<Button
android:id=
"@+id/random"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:onClick=
"onClick"
android:text=
"@string/random"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
app/src/main/res/values/strings.xml
View file @
82a72def
<resources>
<string
name=
"app_name"
>
FloodGame
</string>
<string
name=
"greeting"
>
Hello class, this is a test.
</string>
<string
name=
"greeting"
>
ABC
</string>
<string
name=
"counter_is"
>
Counter is ((%d))
</string>
<string
name=
"decrement"
>
Decrement
</string>
<string
name=
"increment"
>
Increment
</string>
<string
name=
"decrement"
>
Decr
</string>
<string
name=
"increment"
>
Incr
</string>
<string
name=
"reset"
>
Reset
</string>
<string
name=
"random"
>
Rand
</string>
</resources>
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