1

我正在开发一种记忆游戏应用程序,最初的 6 个按钮将显示一些数字。然后当下面的 MyCount5sec 和处理程序运行时,在用户记住 5 秒的等待时间后,处理程序将调用 txtClearRun 从而擦除每个按钮上的所有文本。

然后在 5 秒的等待期之后,进入测试期。用户必须按顺序选择按钮,即。后面的按钮值必须大于前面的按钮值。

如果用户成功(即按下所有 6 个数字按钮),应用程序应该根据 postDelay 等待 1.5 秒?然而,似乎没有任何延误。

如果用户是错误的,即。按下值小于前一个按钮的按钮,用户失败,应用程序也应该等待 1.5 秒,按钮以红色突出显示。但是,似乎也没有这样的 1.5 秒延迟。也没有红色突出显示。

问题

  1. 为什么没有出现延迟?不能同时调用两个处理程序?
  2. 如何进一步修改?

非常感谢!!

   private void setQup() 
   {    
          .....

          MyCount5sec counter5sec = new MyCount5sec(6000,1000);
           counter5sec.start(); 
           handler.postDelayed(txtClearRun, 6000);

           pressed = 0;
           temp = 0;
           final int txtClearRun1time = 1500; // set here!
           button_space1.setText("reset press "+pressed);

           Button11.setOnClickListener(new OnClickListener() {@Override 
                  public void onClick(View v) {vibrate(); int i=0; Button11.setEnabled(false);
                    if (i == B0) {puttobutton(B0,0);} if (i == B1) {puttobutton(B1,1);} if (i == B2) {puttobutton(B2,2);}
                    if (i == B3) {puttobutton(B3,3);} if (i == B4) {puttobutton(B4,4);} if (i == B5) {puttobutton(B5,5);}
                    pressed = pressed + 1;
                    int buttonvalue = Integer.parseInt(Button11.getText().toString());                    
                    if (pressed >5) 
                    {
                        TotalScores=TotalScores+20;
                        handler1.postDelayed(txtClearRun1, txtClearRun1time);
                        loadNextQup();                      
                    }                   
                    if (buttonvalue < temp) 
                    {
                        Button11.setBackgroundResource(R.drawable.red_btn);
                        TotalScores=TotalScores-10;
                        handler1.postDelayed(txtClearRun1, txtClearRun1time);                       
                        loadNextQup();
                    }
                    temp = buttonvalue;
                    }});

    ......same for other buttons..
    }


   Runnable txtClearRun = new Runnable()
   {
       public void run() {blankbutton();} //remove text on buttons
   };

   Runnable txtClearRun1 = new Runnable()
   {
       public void run() {} // solely for wish to delay operations
   };

   private void loadNextQup()
   {

       if (TerminateOrNot ==0) 
       {
           handler.removeCallbacks(txtClearRun);
           handler1.removeCallbacks(txtClearRun1);
           setQup();
       }
         ....
       }
4

1 回答 1

0

我尝试根据您的规则制作一个简单的游戏;)我使用检查背景颜色和文本而不是形状。我希望你能理解它,也许这会对你有所帮助。

这是完整的工作项目:

班级:

public class MainActivity extends Activity {

Button bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9, restart;
List<Button> listButtons;
Handler handler1;
List<Integer> buttonColors;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listButtons = new ArrayList<Button>();
    listButtons.add(bt1 = (Button) findViewById(R.id.bt1));
    listButtons.add(bt2 = (Button) findViewById(R.id.bt2));
    listButtons.add(bt3 = (Button) findViewById(R.id.bt3));
    listButtons.add(bt4 = (Button) findViewById(R.id.bt4));
    listButtons.add(bt5 = (Button) findViewById(R.id.bt5));
    listButtons.add(bt6 = (Button) findViewById(R.id.bt6));
    listButtons.add(bt7 = (Button) findViewById(R.id.bt7));
    listButtons.add(bt8 = (Button) findViewById(R.id.bt8));
    listButtons.add(bt9 = (Button) findViewById(R.id.bt9));

    setAllEnabled(false);

    // add on click listeners
    for (int i = 0; i < listButtons.size(); i++) {
        final int index = i;

        //final because i will need it deeper
        final Button s = listButtons.get(i);

        //on click
        s.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                //when user click on something then show the origini color and tap "x"
                s.setBackgroundColor(buttonColors.get(index));
                s.setText("x");

                // get all checked
                List<Button> listChecked = countChecked();

                // if checked more == 2
                if (listChecked.size() == 2) {

                    // check if the same text was checked
                    boolean btnsHasTheSameTextAndBackgroundTag = checkAllTextAndBackgroundTag(listChecked);

                    //if they dont have same color and text then reset them
                    if (!btnsHasTheSameTextAndBackgroundTag) {
                        for (Button b : listChecked) {
                            b.setText("");
                            b.setBackgroundColor(Color.parseColor("#565656"));
                        }
                    }
                }

                //finall if user clicked 3 the same colors
                else if (listChecked.size() == 3) {

                    // check if the same text was checked
                    boolean btnsHasTheSameTextAndBackgroundTag = checkAllTextAndBackgroundTag(listChecked);

                    if (btnsHasTheSameTextAndBackgroundTag) {
                        for (Button b : listChecked) {
                            b.setVisibility(View.INVISIBLE);
                            b.setText("");
                        }
                    } else {
                        for (Button b : listChecked)
                            {b.setText("");
                            b.setBackgroundColor(Color.parseColor("#565656"));
                            }
                    }
                }

            }

            private boolean checkAllTextAndBackgroundTag(List<Button> list) {
                int number = (Integer) list.get(0).getTag();

                for (Button b : list) {
                    // if one of them will fail
                    if (!b.getText().equals("x") || number != (Integer)b.getTag())
                        return false;
                }
                return true;
            }

            private List<Button> countChecked() {
                List<Button> temp = new ArrayList<Button>();

                for (Button b : listButtons) {
                    if (b.getText().equals("x"))
                        temp.add(b);
                }
                return temp;
            }
        });
    }

    restart = (Button) findViewById(R.id.bt_restart);

    // we go on.. create list of colors
    final List<Integer> colorList = new ArrayList<Integer>();
    colorList.add(Color.parseColor("#C3C3E5"));
    colorList.add(Color.parseColor("#F1F0FF"));
    colorList.add(Color.parseColor("#8C489F"));

    // on restart button action
    restart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //enable all
            setAllEnabled(true);

            // remember new colors and create for it container
            buttonColors = new ArrayList<Integer>();

            // create random
            Random r = new Random();

            // get temp counter used colors
            int[] wastedColor = new int[colorList.size()];

            // for all buttons do the same job
            for (int i = 0; i < listButtons.size(); i++) {

                Log.d("xxx", i + "");

                // take first random number
                int numberRandom = r.nextInt(colorList.size());

                // check if we have all colors with the same amount
                while (wastedColor[numberRandom] == colorList.size())
                    numberRandom = r.nextInt(colorList.size());

                // increment existing ones
                wastedColor[numberRandom]++;

                // remmeber this color
                buttonColors.add(colorList.get(numberRandom));

                listButtons.get(i).setTag(numberRandom);
                listButtons.get(i).setBackgroundColor((int) (colorList.get(numberRandom)));
                listButtons.get(i).setVisibility(View.VISIBLE);

            }

            // after generated colors...

            handler1 = new Handler();

            // hide after 5 sec
            handler1.postDelayed(new HideAllColors(), 5000);

        }
    });

}

private void setAllEnabled(boolean status) {
    for(Button b : listButtons)
        b.setEnabled(status);

}

private class HideAllColors implements Runnable {

    @Override
    public void run() {
        for (Button b : listButtons)
            b.setBackgroundColor(Color.parseColor("#565656"));

    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/bt1"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="5dp"
        android:background="#565656" />

    <Button
        android:id="@+id/bt2"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="5dp"
        android:layout_toRightOf="@+id/bt1"
        android:background="#565656" />

    <Button
        android:id="@+id/bt3"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="5dp"
        android:layout_toRightOf="@+id/bt2"
        android:background="#565656" />

    <Button
        android:layout_margin="5dp"
        android:id="@+id/bt4"
        android:layout_below="@+id/bt1"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="#565656" />

    <Button
        android:layout_margin="5dp"
        android:id="@+id/bt5"
        android:layout_below="@+id/bt2"
        android:layout_toRightOf="@+id/bt4"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="#565656" />

    <Button
        android:layout_margin="5dp"
        android:id="@+id/bt6"
        android:layout_below="@+id/bt3"
        android:layout_toRightOf="@+id/bt5"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="#565656" />

    <Button
        android:layout_margin="5dp"
        android:id="@+id/bt7"
        android:layout_below="@+id/bt4"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="#565656" />

    <Button
        android:layout_margin="5dp"
        android:id="@+id/bt8"
        android:layout_below="@+id/bt5"
        android:layout_toRightOf="@+id/bt7"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="#565656" />

    <Button
        android:layout_margin="5dp"
        android:id="@+id/bt9"
        android:layout_below="@+id/bt6"
        android:layout_toRightOf="@+id/bt8"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="#565656" />


    <Button
        android:layout_margin="5dp"
        android:id="@+id/bt_restart"
        android:text="restart"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#565656" />






</RelativeLayout>
于 2012-12-18T20:17:09.147 回答