dimanche 25 octobre 2015

Random generating in Android(2)

I want to change that when generating the last number, the app will do the sorting except the last number. And the numbers cannot repeat.After generating the last number, click the "NEXT NUMBER" button will have no response. You need to click "clear" button to restart the app. I have tries so many times and it still cannot work. Usually, the app is stopped or cannot generate the numbers one by one.

MainActivity.java

public class MainActivity extends AppCompatActivity {
TextView[] views = new TextView[7];
int[] num = new int[7];
int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    views[0] = ((TextView) findViewById(R.id.tv_number_one));
    views[1] = ((TextView) findViewById(R.id.tv_number_two));
    views[2] = (TextView)findViewById(R.id.tv_number_three);
    views[3] = (TextView)findViewById(R.id.tv_number_four);
    views[4] = (TextView)findViewById(R.id.tv_number_five);
    views[5] = (TextView)findViewById(R.id.tv_number_six);
    views[6] = (TextView)findViewById(R.id.tv_number_seven);

}
@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, 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);
}
public void generate(View v)
{
    Random myRandom = new Random();
    if (count >= 7) {
        clear(views[0]);
        clear(views[1]);
        clear(views[2]);
        clear(views[3]);
        clear(views[4]);
        clear(views[5]);
        clear(views[6]);
        for (TextView tv : views) tv.setText("?");
    }
    num[count] = myRandom.nextInt(48) + 1;
    views[count].setText(String.valueOf(num[count]));
    count++;
}
public void clear(View v){
    num = new int[7];
    for (TextView tv : views) tv.setText("?");
    count = 0;
}
}

activity_main

 <TextView android:text="\?" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_number_one"
    android:textSize="30sp"
    android:textIsSelectable="true"
    android:layout_marginLeft="10dp"
    android:textColor="#67ceff"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_two"
    android:layout_alignBottom="@+id/tv_number_one"
    android:layout_toRightOf="@+id/tv_number_one"
    android:layout_toEndOf="@+id/tv_number_one"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_three"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_two"
    android:layout_toEndOf="@+id/tv_number_two"
    android:layout_marginLeft="20dp"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_four"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_three"
    android:layout_toEndOf="@+id/tv_number_three"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:textIsSelectable="true"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_five"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_four"
    android:layout_toEndOf="@+id/tv_number_four"
    android:layout_marginLeft="20dp"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_six"
    android:layout_alignBottom="@+id/tv_number_five"
    android:layout_toRightOf="@+id/tv_number_five"
    android:layout_toEndOf="@+id/tv_number_five"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_seven"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_six"
    android:layout_toEndOf="@+id/tv_number_six"
    android:layout_marginLeft="20dp"
    android:textColor="#6198ff"
    android:textSize="30sp"
    android:layout_marginStart="20dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="NEXT NUMBER"
    android:id="@+id/button_next_num"
    android:layout_below="@+id/tv_number_one"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="41dp"
    android:onClick="generate"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CLEAR"
    android:id="@+id/button_clear"
    android:onClick="clear"
    android:layout_alignTop="@+id/button_next_num"
    android:layout_toRightOf="@+id/tv_number_six"
    android:layout_toEndOf="@+id/tv_number_six"
    android:layout_marginLeft="30dp" />




Aucun commentaire:

Enregistrer un commentaire