我有一个包含自定义视图的xml布局:

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:orientation="vertical"
        android:layout_weight="50"
    >
        <org.example.sudoku.PuzzleView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/puzzlebackground"
            android:id="@+id/puzzleId"/>

    </LinearLayout>


我有一个活动,将这种布局设置为其内容,并调用一个对话框以供用户输入,该对话框又调用了PuzzleView中的一个函数以最终更改PuzzleView的内容。问题在于,关闭对话框后,更改尚未重新绘制。相反,它会在用户的下一个其他输入上重绘。
以下是一些可以帮助您的代码段:

Game.java:

// ...
public void showKeypadOrError(int x, int y)
{
    int tiles[] = getUsedTiles(x,y);
    if (tiles.length == 9)
    {
        Toast toast = Toast.makeText(this, R.string.no_moves_label, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }else
    {
        Log.d(TAG, "showKeypad: used=" + toPuzzleString(tiles));
        Dialog v = new Keypad(this, this.puzzleView, x, y);
        v.show();
    }
}
public void setTile(int x, int y, int value) {
    puzzle[y * 9 + x] = value;
}


Keypad.java:

private void setListeners()
{
    for (int i = 0; i < keys.length; i++)
    {
        final int t = i + 1;
        keys[i].setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                returnResult(t);
            }
        });
    }
}

private void returnResult(int tile)
{
    puzzleView.setSelectedTile(selX, selY, tile);
    puzzleView.invalidate();
    dismiss();
}


最后,PuzzleView.java

public void setSelectedTile(int X, int Y, int tile) {
    game.setTile(X, Y, tile);
    //this.invalidate();// may change hints
}


我发现了一个看起来与我的问题相似的问题,但我不知道解决方案是什么:

How to invalidate() on return from a dialog?

非常感谢您的帮助!
编辑:添加更多代码-希望此帮助:
PuzzleView.java

// Handle input in touch mode
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() != MotionEvent.ACTION_DOWN) {
        return super.onTouchEvent(event);
    }

    // Allow player to choose the tile that defined by game
    // But only allow the user to modify the tiles that are blank
    select((int) (event.getX() / width),
            (int) (event.getY() / height));

    int[] predefined = new int[81];
    // Get the tile that are not blank (predefined by game)
    predefined = game.getPredefinedTileFromPuzzle();
    // Check if the selected tile is whether predefined or not
    if (predefined[selY * 9 + selX] == 1) {
        return true;
    }
    //Call the Dialog input from Game.java
    game.showKeypadOrError(selX, selY);
    Log.i(TAG, "Right before terminate onTouchEvent()");
    return true;
}

最佳答案

取消对话框后,您应该使之无效。

实现DialogInterface.OnDismissListener接口,并在OnDismiss回调中使之无效。

    Dialog v = new Keypad(this, this.puzzleView, x, y);

    v.setOnDismissListener(new OnDismissListener() {
    @Override
        public void onDismiss(final DialogInterface arg0) {
            puzzleView.postInvalidate ();
        }
    });

     v.show();

10-05 19:03