我实现了一个“警报对话框”,但是有一个问题。此对话框重复三次:启动应用程序时,两次启动游戏(使用微调框和意图后)。我的要求是,该“警报”对话框必须只显示一次(也许是在我打开游戏时才显示),但如果在应用程序的开头将其打开也将很好。谢谢大家这是我的代码:
Spinner s = (Spinner) findViewById(R.id.Spinner01);
@SuppressWarnings("rawtypes")
ArrayAdapter adapter = ArrayAdapter.createFromResource(
this, R.array.type, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
scelta = flags;
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
scelta = flags;
}
});
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
scelta = pokemon;
}
});
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
scelta = smiles;
}
});
button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
scelta = MU;
}
});
s.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(
android.widget.AdapterView<?> arg0,
View arg1, int pos, long arg3){
AlertDialog.Builder miaAlert = new AlertDialog.Builder(Manager.this);
miaAlert.setTitle("Which is the difficulty?");
miaAlert.setCancelable(false);
miaAlert.setPositiveButton("Facile", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
easy = 10;
}
});
miaAlert.setNegativeButton("Difficile", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
hard = -5;
}
});
AlertDialog alert = miaAlert.create();
alert.show();
// ((Spinner) findViewById(R.id.Spinner01)).setSelection(0);
Hard和Easy是我用来增加和减少玩家在游戏中的可能性的两个int变量
最佳答案
如果s
是R.id.Spinner01
,则
((Spinner) findViewById(R.id.Spinner01)).setSelection(0);
导致导致您的异常的循环。您应该避免在
setSelection
内部调用onItemSelected
,因为setSelection
会触发onItemSelected