处理块外的变量

时间:2012-07-16 03:22:18

标签: android variables

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

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

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





}
}

2 个答案:

答案 0 :(得分: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);
      //
    }
}

答案 1 :(得分: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 {

/ **首次创建活动时调用。 * / @覆盖 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.