0

我有一个应用程序设置为有十八个按钮。这背后的基本思想是,如果单击其中三个按钮,则会为每个按钮分配一个字符串变量。然后将这些值相加并与字符串进行比较。如果字符串的值匹配,我希望出现一个警告对话框。我尝试将每个按钮变量存储在共享首选项中,但警报对话框仅在您重新启动该活动后才会显示。我希望这立即发生。如果有人能对此有所了解,我将永远感激不尽。此外,如果可以采用更有效的方法来做到这一点,那将很有帮助。

我不确定如何处理按钮等块之外的变量。如果我知道怎么做,我只需将三个变量传递到按钮块之外并在同一个文件中调用它,比较然后设置警报。同样,基本逻辑是:如果选择了三个特定按钮并且选择了所有三个按钮,那么只有在那时才会出现警报对话框。(如果有帮助,选择的任何其他按钮都会重置所有其他按钮)。代码:

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();
    }





}
}
4

2 回答 2

0

将所有这些局部变量声明为字段:

public class Stage1Level1Activity extends Activity {
   //fields  
   private ImageButton a1;
   ....
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.stage1level1);

      a1 = (ImageButton) findViewById(R.id.a1);
      //
    }
}
于 2012-07-16T03:25:49.597 回答
0

如果您在包含所有这些处理程序的“类”中创建变量,它们都可以直接访问变量。

所以试试这个:

public class Stage1Level1Activity extends Activity {
    private String myVariable;

然后,您应该能够在您定义的任何这些方法中访问 myVariable

(社论如下,不是答案的一部分)问题是这段代码真的应该是 10-15 行,认真的(而且不是密集的难以理解的行!)。我不能完全为你做这件事,因为我没有 GUI 工具,但例如,至少为了让你开始(所以你可以明白我的意思):

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);
    }

相当于:

OnClickListener ocLictener=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);
    }
    });
b3.setOnClickListener(ocListener);
b4.setOnClickListener(ocListener);

(请注意,我刚刚选择了 2 个遵循完全相同的模式,还有更多。

我并不是要批评(好吧,我想我是,但我并不是以卑鄙的方式,只是有启发性),我只是在想,如果您继续按照这些思路进行编码,您将使自己陷入困境很多心痛。我会将整个剪辑发布在“重构”SO 网站上,看看会发生什么:)

顺便说一句,问题的一部分是 GUI 构建器,他们鼓励这种代码,但他们不需要它......

编辑:实际上我不能不管这个,让我尝试一些重构:

公共类 Stage1Level1Activity 扩展 Activity {

/** 在第一次创建活动时调用。*/ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.stage1level1);

// Here we create a bunch of image buttons.  Instead of having he same line repeated
// let's put them into an array.  I forget the type of Android's resources but let's
// assume they are Strings:

// Need to list all the ids somehow, this is probably the easiest.
private String[] ids=new String[]{R.id.a1, R.id.a2, r.id.a3, ..., r.id.e4}  
private String[] triggerIds=new String[]{R.id.b2, R.id.c2, r.id.d2}    
private ImageButton btns=new ImageButton[ids.length];
private ImageButton triggerBtns=new ImageButton[ids.length];

for(int i=0;i<ids.length;i++)
    btns[i]= (ImageButton) findViewById(ids[i]);
for(int i=0;i<triggerIds.length;i++)
    triggerBtns[i]=(ImageButton) findViewById(ids[i]);

// Now you have two types of buttons, the first type are all the same
for(ImageButton ib:btns) {
    ib.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            for(ImageButton tb:triggerBtns) {
                tb.setBackgroundResource(R.drawable.green_empty);
    }
    });
}

int arrayPtr=0;  // Used to fill out some values in the following loop

for(ImageButton tb:triggerBtns) {
    tb.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            tb.setBackgroundResource(R.drawable.green_filled);
            SharedPreferences sharedPreferences1 = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences1.edit();
            // Right here is the only place that these three differ.  The pattern is:
            // editor.putString("part1", "a");
            // editor.putString("part2", "b");
            // editor.putString("part3", "c");
            String[] values=new String[]{"part1", "a", "part2","b","part3","c"};
            editor.putString(values[arrayPtr++], values[arrayPtr++]);
            editor.commit();
        }
    });
// I'll leave it here, that replaces at least 4/5 of the original code and in 20
// or so lines.  A lot more could be done, but the array trick is really handy
// for code like this.
于 2012-07-16T03:41:30.227 回答