Handling 20-face dice
This commit is contained in:
parent
531ebd6646
commit
e0ed947d30
@ -7,6 +7,7 @@ import android.widget.Button;
|
|||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by thibaud on 20/04/17.
|
* Created by thibaud on 20/04/17.
|
||||||
@ -15,14 +16,17 @@ import java.util.Random;
|
|||||||
public class DiceActivity extends Activity implements View.OnClickListener {
|
public class DiceActivity extends Activity implements View.OnClickListener {
|
||||||
|
|
||||||
private TextView textResult;
|
private TextView textResult;
|
||||||
|
private int max;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_dice);
|
setContentView(R.layout.activity_dice);
|
||||||
|
|
||||||
|
max = getIntent().getIntExtra("max", 0);
|
||||||
|
|
||||||
TextView textTitle = (TextView)findViewById(R.id.textTitle);
|
TextView textTitle = (TextView)findViewById(R.id.textTitle);
|
||||||
textTitle.setText("6-sided dice");
|
textTitle.setText(String.valueOf(max) + "-sided dice");
|
||||||
|
|
||||||
textResult = (TextView)findViewById(R.id.textResult);
|
textResult = (TextView)findViewById(R.id.textResult);
|
||||||
textResult.setText("");
|
textResult.setText("");
|
||||||
@ -34,7 +38,7 @@ public class DiceActivity extends Activity implements View.OnClickListener {
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View v){
|
public void onClick(View v){
|
||||||
Random rand = new Random();
|
Random rand = new Random();
|
||||||
int result = rand.nextInt(6) + 1;
|
int result = rand.nextInt(max) + 1;
|
||||||
this.textResult.setText(result);
|
this.textResult.setText(String.valueOf(result));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
Intent intent = new Intent(MainActivity.this, DiceActivity.class);
|
Intent intent = new Intent(MainActivity.this, DiceActivity.class);
|
||||||
|
intent.putExtra("max", 6);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -27,6 +28,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
Intent intent = new Intent(MainActivity.this, DiceActivity.class);
|
Intent intent = new Intent(MainActivity.this, DiceActivity.class);
|
||||||
|
intent.putExtra("max", 20);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user