From 531ebd6646d96ece7a9debd3b24e950ce6ba36b2 Mon Sep 17 00:00:00 2001 From: thibaud Date: Thu, 20 Apr 2017 14:04:52 +0200 Subject: [PATCH] DiceActivity --- .../com/example/diceroller/DiceActivity.java | 25 ++++++++++++++++++- .../com/example/diceroller/MainActivity.java | 21 ++++++++++++++++ app/src/main/res/layout/activity_dice.xml | 1 + 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/example/diceroller/DiceActivity.java b/app/src/main/java/com/example/diceroller/DiceActivity.java index d1873d7..894fcb5 100644 --- a/app/src/main/java/com/example/diceroller/DiceActivity.java +++ b/app/src/main/java/com/example/diceroller/DiceActivity.java @@ -2,16 +2,39 @@ package com.example.diceroller; import android.app.Activity; import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import java.util.Random; /** * Created by thibaud on 20/04/17. */ -public class DiceActivity extends Activity { +public class DiceActivity extends Activity implements View.OnClickListener { + + private TextView textResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dice); + + TextView textTitle = (TextView)findViewById(R.id.textTitle); + textTitle.setText("6-sided dice"); + + textResult = (TextView)findViewById(R.id.textResult); + textResult.setText(""); + + Button rollButton = (Button)findViewById(R.id.rollButton); + rollButton.setOnClickListener(this); + } + + @Override + public void onClick(View v){ + Random rand = new Random(); + int result = rand.nextInt(6) + 1; + this.textResult.setText(result); } } diff --git a/app/src/main/java/com/example/diceroller/MainActivity.java b/app/src/main/java/com/example/diceroller/MainActivity.java index 08a4d5f..98dea24 100644 --- a/app/src/main/java/com/example/diceroller/MainActivity.java +++ b/app/src/main/java/com/example/diceroller/MainActivity.java @@ -1,7 +1,10 @@ package com.example.diceroller; +import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.view.View; +import android.widget.Button; public class MainActivity extends AppCompatActivity { @@ -9,5 +12,23 @@ public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + Button button6 = (Button)findViewById(R.id.button6); + button6.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(MainActivity.this, DiceActivity.class); + startActivity(intent); + } + }); + + Button button20 = (Button)findViewById(R.id.button20); + button20.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(MainActivity.this, DiceActivity.class); + startActivity(intent); + } + }); } } diff --git a/app/src/main/res/layout/activity_dice.xml b/app/src/main/res/layout/activity_dice.xml index 0c7436d..314cc11 100644 --- a/app/src/main/res/layout/activity_dice.xml +++ b/app/src/main/res/layout/activity_dice.xml @@ -21,6 +21,7 @@ android:textSize="250sp"/>