我有一个应用程序设置为有十八个按钮。这背后的基本思想是,如果单击其中三个按钮,则会为每个按钮分配一个字符串变量。然后将这些值相加并与字符串进行比较。如果字符串的值匹配,我希望出现一个警告对话框。我尝试将每个按钮变量存储在共享首选项中,但警报对话框仅在您重新启动该活动后才会显示。我希望这立即发生。如果有人能对此有所了解,我将永远感激不尽。此外,如果可以采用更有效的方法来做到这一点,那将很有帮助。
我不确定如何处理按钮等块之外的变量。如果我知道怎么做,我只需将三个变量传递到按钮块之外并在同一个文件中调用它,比较然后设置警报。同样,基本逻辑是:如果选择了三个特定按钮并且选择了所有三个按钮,那么只有在那时才会出现警报对话框。(如果有帮助,选择的任何其他按钮都会重置所有其他按钮)。代码:
public class Stage1Level1Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stage1level1);
ImageButton a1 = (ImageButton) findViewById(R.id.a1);
ImageButton a3 = (ImageButton) findViewById(R.id.a3);
ImageButton a4 = (ImageButton) findViewById(R.id.a4);
ImageButton b1 = (ImageButton) findViewById(R.id.b1);
final ImageButton b2 = (ImageButton) findViewById(R.id.b2);
ImageButton b3 = (ImageButton) findViewById(R.id.b3);
ImageButton b4 = (ImageButton) findViewById(R.id.b4);
ImageButton c1 = (ImageButton) findViewById(R.id.c1);
final ImageButton c2 = (ImageButton) findViewById(R.id.c2);
ImageButton c3 = (ImageButton) findViewById(R.id.c3);
ImageButton c4 = (ImageButton) findViewById(R.id.c4);
ImageButton d1 = (ImageButton) findViewById(R.id.d1);
final ImageButton d2 = (ImageButton) findViewById(R.id.d2);
ImageButton d3 = (ImageButton) findViewById(R.id.d3);
ImageButton d4 = (ImageButton) findViewById(R.id.d4);
ImageButton e1 = (ImageButton) findViewById(R.id.e1);
ImageButton e3 = (ImageButton) findViewById(R.id.e3);
ImageButton e4 = (ImageButton) findViewById(R.id.e4);
a1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b2.setBackgroundResource(R.drawable.green_empty);
c2.setBackgroundResource(R.drawable.green_empty);
d2.setBackgroundResource(R.drawable.green_empty);
}
});
a3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b2.setBackgroundResource(R.drawable.green_empty);
c2.setBackgroundResource(R.drawable.green_empty);
d2.setBackgroundResource(R.drawable.green_empty);
}
});
a4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b2.setBackgroundResource(R.drawable.green_empty);
c2.setBackgroundResource(R.drawable.green_empty);
d2.setBackgroundResource(R.drawable.green_empty);
}
});
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b2.setBackgroundResource(R.drawable.green_empty);
c2.setBackgroundResource(R.drawable.green_empty);
d2.setBackgroundResource(R.drawable.green_empty);
}
});
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b2.setBackgroundResource(R.drawable.green_filled);
SharedPreferences sharedPreferences1 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences1.edit();
editor.putString("part1", "a");
editor.commit();
}
});
b3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b2.setBackgroundResource(R.drawable.green_empty);
c2.setBackgroundResource(R.drawable.green_empty);
d2.setBackgroundResource(R.drawable.green_empty);
}
});
b4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b2.setBackgroundResource(R.drawable.green_empty);
c2.setBackgroundResource(R.drawable.green_empty);
d2.setBackgroundResource(R.drawable.green_empty);
}
});
c1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b2.setBackgroundResource(R.drawable.green_empty);
c2.setBackgroundResource(R.drawable.green_empty);
d2.setBackgroundResource(R.drawable.green_empty);
}
});
c2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
c2.setBackgroundResource(R.drawable.green_filled);
SharedPreferences sharedPreferences2 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences2.edit();
editor.putString("part2", "b");
editor.commit();
}
});
c3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b2.setBackgroundResource(R.drawable.green_empty);
c2.setBackgroundResource(R.drawable.green_empty);
d2.setBackgroundResource(R.drawable.green_empty);
}
});
c4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b2.setBackgroundResource(R.drawable.green_empty);
c2.setBackgroundResource(R.drawable.green_empty);
d2.setBackgroundResource(R.drawable.green_empty);
}
});
d1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b2.setBackgroundResource(R.drawable.green_empty);
c2.setBackgroundResource(R.drawable.green_empty);
d2.setBackgroundResource(R.drawable.green_empty);
}
});
d2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
d2.setBackgroundResource(R.drawable.green_filled);
SharedPreferences sharedPreferences3 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences3.edit();
editor.putString("part3", "c");
editor.commit();
}
});
d3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b2.setBackgroundResource(R.drawable.green_empty);
c2.setBackgroundResource(R.drawable.green_empty);
d2.setBackgroundResource(R.drawable.green_empty);
}
});
d4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b2.setBackgroundResource(R.drawable.green_empty);
c2.setBackgroundResource(R.drawable.green_empty);
d2.setBackgroundResource(R.drawable.green_empty);
}
});
e1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b2.setBackgroundResource(R.drawable.green_empty);
c2.setBackgroundResource(R.drawable.green_empty);
d2.setBackgroundResource(R.drawable.green_empty);
}
});
e3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b2.setBackgroundResource(R.drawable.green_empty);
c2.setBackgroundResource(R.drawable.green_empty);
d2.setBackgroundResource(R.drawable.green_empty);
}
});
e4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b2.setBackgroundResource(R.drawable.green_empty);
c2.setBackgroundResource(R.drawable.green_empty);
d2.setBackgroundResource(R.drawable.green_empty);
}
});
SharedPreferences sharedPreferences1 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
String part1 = sharedPreferences1.getString("part1", "");
SharedPreferences sharedPreferences2 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
String part2 = sharedPreferences2.getString("part2", "");
SharedPreferences sharedPreferences3 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
String part3 = sharedPreferences3.getString("part3", "");
String added = part1 + part2 + part3;
String compared = "abc";
if (added.equalsIgnoreCase(compared) ){
AlertDialog alertDialog = new AlertDialog.Builder(Stage1Level1Activity.this).create();
alertDialog.setTitle("Reset...");
alertDialog.setMessage("R u sure?");
alertDialog.setButton2("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
} });
alertDialog.show();
}
}
}