DiceActivity

This commit is contained in:
thibaud 2017-04-20 14:04:52 +02:00
parent 7e23e9b29b
commit 531ebd6646
3 changed files with 46 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -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);
}
});
}
}

View File

@ -21,6 +21,7 @@
android:textSize="250sp"/>
<Button
android:id="@+id/rollButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/roll_dice"