在Android中的新游戏中点击按钮

时间:2015-02-26 19:33:40

标签: android button gridview

我是Android开发的新手,并且做了一个简单的Hangman游戏。

我有一个gridview,每个字母(A-Z)都有按钮。点击后每个按钮都变得不可点击,当新单词打开时我想再次点击它们。我怎样才能有效地做到这一点?我不希望将每个单击的按钮保留在列表中并解析它以使它们可单击。

1 个答案:

答案 0 :(得分:0)

        private void fillGridView() {

            ButtonClickHandler clickHandler = new ButtonClickHandler();

            String[] resources = getResources().getStringArray(R.array.button_characters);

            gridView = (GridView) findViewById(R.id.gridView);
            gridView.setAdapter(new CustomGridAdapter(this, clickHandler, resources));

        }
        private void play() {

                remainingLife = 6;

                if (isFirstPlay) {
                    word = organizer.getWord(GameOrganizer.Action.DONTDELETE);
                    isFirstPlay = false;
                } else {
                    word = organizer.getWord(GameOrganizer.Action.DELETEFIRST);
                    changeImage();
                }

                fillGridView();

                if (word != null)
                    prepareEditText(word);
                else
                    Toast.makeText(this, "KALMADI ULAN :(", Toast.LENGTH_SHORT).show();
            }
private void openPopup(){
        Button b = new Button(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
        AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
        builder.setMessage("Do you want to play again ?").setTitle("End of the Game");

        builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                play();
            }
        });
        builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                finish();
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private class ButtonClickHandler implements View.OnClickListener {
            @Override
            public void onClick(View v) {

                if (remainingLife <= 0) {
                    Toast.makeText(getApplicationContext(), "You dont have any life.", Toast.LENGTH_SHORT).show();
                    return;
                }

                String identifier = ((String) v.getTag()).replaceAll("[^A-Z/Ş/Ö/I/İ/Ğ/Ç/Ü]+", "");
                if (word.contains(identifier)) {
                    int index = word.indexOf(identifier);

                    while (index != -1) {
                        seenWordBuilder.setCharAt((2 * index), identifier.charAt(0));
                        index = word.indexOf(identifier, index + 1);
                    }
                    text.setText(seenWordBuilder);

                } else {
                    remainingLife--;
                    changeImage();
                }

                if (!seenWordBuilder.toString().contains("_")) {
                    openPopup();
                }

                v.setClickable(false);
                v.setBackgroundResource(R.drawable.unclickablegridbuttonshape);
            }

我在play方法中使用了fillGridView方法调用。因此,它被称为在第一次打开时和其他时间绘制gridview。我觉得这有点糟糕。

我解释了customgridview它是由BaseAdapter扩展的,它在getView方法中创建了按钮。