我实施了警报对话,但我遇到了问题。此对话框重复三次:当应用程序启动时,两次我开始游戏时(使用微调器和意图后)。我的要求是这个警告对话框必须只显示一次(也许当我在意图之后打开游戏时)但是如果它将在应用程序的开头打开它也会很好。谢谢大家。这里有我的代码:
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 and Easy是我用来增加和减少游戏中玩家可能性的两个int变量
答案 0 :(得分:1)
如果s
是R.id.Spinner01
,则
((Spinner) findViewById(R.id.Spinner01)).setSelection(0);
导致导致异常的循环。您应该避免在setSelection
内拨打onItemSelected
,因为setSelection
会触发onItemSelected