lundi 23 février 2015

How to randomly create and assign variables and shorten an if-else statement (Java android)

This is my first question asked on StackOverflow. I was wondering how to shorten my code by automatically creating and assigning variables for TextViews. Also, how do you shorten these if-else statements? I have looked at other threads, but they haven't fulfilled my needs. I am using Android Studio for this.


Here is my Java code:



package com.example.ani.testproject;

import android.graphics.Color;
import android.os.SystemClock;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;


public class MainScreen extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
final TextView spot1 = (TextView) findViewById(R.id.spot1);
final TextView spot2 = (TextView) findViewById(R.id.spot2);
final TextView spot3 = (TextView) findViewById(R.id.spot3);
final TextView spot4 = (TextView) findViewById(R.id.spot4);
final TextView spot5 = (TextView) findViewById(R.id.spot5);
final TextView spot6 = (TextView) findViewById(R.id.spot6);
final TextView spot7 = (TextView) findViewById(R.id.spot7);
final TextView spot8 = (TextView) findViewById(R.id.spot8);
final TextView spot9 = (TextView) findViewById(R.id.spot9);
final TextView spot10 = (TextView) findViewById(R.id.spot10);
Button button = (Button) findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
int loopnum = 1;
while(loopnum<4){
Random r = new Random();
int spotnumber = r.nextInt(11-1) + 1;
if(spotnumber==1){
spot1.setBackgroundColor(Color.RED);
}
else if(spotnumber==2){
spot2.setBackgroundColor(Color.RED);
}
else if(spotnumber==3){
spot3.setBackgroundColor(Color.RED);
}
else if(spotnumber==4){
spot4.setBackgroundColor(Color.RED);
}
else if(spotnumber==5){
spot5.setBackgroundColor(Color.RED);
}
else if(spotnumber==6){
spot6.setBackgroundColor(Color.RED);
}
else if(spotnumber==7){
spot7.setBackgroundColor(Color.RED);
}
else if(spotnumber==8){
spot8.setBackgroundColor(Color.RED);
}
else if(spotnumber==9){
spot9.setBackgroundColor(Color.RED);
}
else if(spotnumber==10){
spot10.setBackgroundColor(Color.RED);
}
loopnum += 1;
SystemClock.sleep(5000);
}
}
});



}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_screen, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


I was not really able to show this clearly, but the last bracket corresponds to the bracket in:



public class MainScreen extends ActionBarActivity {


Here is my XML code:



<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation = "horizontal">

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="170dp"
android:layout_height="match_parent"
android:orientation = "vertical"
android:layout_marginTop = "5dp"
android:layout_marginRight = "5dp"
android:id="@+id/linearLayout">

<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot1"
android:layout_marginTop="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot2"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot3"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot4"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot5"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"/>


</LinearLayout>


<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="170dp"
android:layout_height="match_parent"
android:orientation = "vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginTop = "5dp"
android:id="@+id/LinearLayout2">

<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Me"
android:id="@+id/button"
android:layout_gravity="right" />

<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot6"
android:layout_marginTop="10dp"
android:layout_alignParentRight = "true"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot7"
android:layout_marginTop="10dp"
android:layout_alignParentRight = "true"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot8"
android:layout_marginTop="10dp"
android:layout_alignParentRight = "true"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot9"
android:layout_marginTop="10dp"
android:layout_alignParentRight = "true"
android:background="#ffffff"/>
<TextView
android:layout_width="90dp"
android:layout_height="90dp"
android:id = "@+id/spot10"
android:layout_marginTop="10dp"
android:layout_alignParentRight = "true"
android:background="#ffffff"/>


</LinearLayout>


Please keep in mind that I am a bit new to Android and Java. Please try not to openly show hate for my code. Thank you in advance.


Aucun commentaire:

Enregistrer un commentaire