所以我试图这样做:当你点击图像时,中心的文字变成了数组中的一个随机元素,但是我希望每一个出现过一次的元素都不再出现......我试着这样做这但它没有用,请在这里帮助我!!
package com.example.bibiwars.skills;
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.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img = (ImageView) findViewById(R.id.logo);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//ToastMessage to test the click ::::::
Toast.makeText(MainActivity.this, "Clicked!!",Toast.LENGTH_SHORT).show();
TextView text = (TextView) findViewById(R.id.text);
String[] Array = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
int rnd = new Random().nextInt(Array.length);
while (Array[rnd].equals("0"))
{rnd = new Random().nextInt(Array.length);}
text.setText(Array[rnd]);
Array[rnd] = "0";
}
});
}
@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);
}
}
答案 0 :(得分:0)
您应该将数组保存为字段变量,并在选择后实际删除一个条目。这样你就不会两次相同。
package com.example.bibiwars.skills;
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.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends ActionBarActivity {
final ArrayList<String> places = = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img = (ImageView) findViewById(R.id.logo);
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//ToastMessage to test the click ::::::
Toast.makeText(MainActivity.this, "Clicked!!",Toast.LENGTH_SHORT).show();
TextView text = (TextView) findViewById(R.id.text);
if(places.size() >0)
{
int rnd = new Random().nextInt(places.size());
text.setText(places.get(rnd));
places.remove(rnd);
}
}
});
}
//... other methods...
}
答案 1 :(得分:0)
shuffle array
// Implementing Fisher–Yates shuffle
static void shuffleArray(int[] ar)
{
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
当你点击show next array element。