1

我今天有一个奇怪的问题。我正在尝试实现一个游戏,其中我有一个带有一些按钮的网格视图。单击任何按钮时,它会被禁用(使用setEnabled(false))。问题是,只有第一个按钮没有被禁用。尽管日志输出显示这setEnabled(false)是需要的。

这是我的代码:

public class ButtonAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private GameScreen gameScreen;
    private Hangman hangman;
    private static final String[] buttonValues = 
        {
            "A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N",
            "O", "P", "Q", "R", "S", "T", "U",
            "",  "V", "W", "X", "Y", "Z", "" ,
        };
    private Button[] holder = new Button[getCount()];

    public ButtonAdapter(GameScreen gameScreen) {
        inflater = (LayoutInflater) gameScreen
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.gameScreen = gameScreen;
    }

    public View getView(final int position, View convertView, ViewGroup arg2) {
        View view = convertView;
        if(view == null) {
            view = inflater.inflate(R.layout.game_button, null);

            final Button button = (Button) view.findViewById(R.id.button);
            button.setText(buttonValues[position]);
            holder[position] = button;
            view.setTag(holder[position]);
        }
        else
            holder[position] = (Button) view.getTag();

        holder[position].setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if(buttonValues[position].length() != 0) {
                    Hangman.GameState gameState = hangman.updateCurrentWord(
                            buttonValues[position].charAt(0));
                    holder[position].setEnabled(false);

                    gameScreen.updateView(gameState);
                    Log.d("Button", position + ":" + holder[position].getText());
                }
            }
        });

        return view;
    }

    public int getCount() {
        return buttonValues.length;
    }

    public Object getItem(int position) {
        return buttonValues[position];
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return holder[position].isEnabled();
    }
}

我也试过((Button) view.findViewById(R.id.button)).setOnClickListener(...);,但没有改变。

4

1 回答 1

4

有时这些事情发生是因为您试图访问尚未膨胀的视图。尝试这个:

    View view = convertView;

    if(view == null) 
    {
        view = inflater.inflate(R.layout.game_button, null);
        vto = view.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {@Override public void onGlobalLayout() 
        {
            final Button button = (Button) view.findViewById(R.id.button);
            button.setText(buttonValues[position]);
            holder[position] = button;
            view.setTag(holder[position]);

            ViewTreeObserver obs = view.getViewTreeObserver();
            obs.removeGlobalOnLayoutListener(this);
        }});
    ...

或者可能把ViewTreeObserver其他地方。这完全取决于if statement代码的哪一侧首先输入。

于 2013-05-01T11:37:14.587 回答