1

我有一个包含自定义视图的 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 的内容。问题是,在 Dialog 关闭后没有重新绘制更改。相反,它会在用户的下一个其他输入上重新绘制。以下是一些可能对您有所帮助的代码片段:

游戏.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;
}

键盘.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
}

我发现了一个与我的问题相似的问题,但我无法弄清楚解决方案是什么:

如何在从对话框返回时使()无效?

非常感谢您的帮助!编辑:添加更多代码-希望对您有所帮助: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;
}
4

2 回答 2

1

您应该在对话框关闭后失效。

实现接口并在回调中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();
于 2012-04-22T04:53:44.407 回答
0

做就是了puzzleView.requestLayout();

于 2014-06-16T17:10:32.627 回答