gpt4 book ai didi

android - invalidate() 不会立即更改自定义 View 的内容

转载 作者:行者123 更新时间:2023-11-30 04:14:28 24 4
gpt4 key购买 nike

我有一个包含自定义 View 的 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>

我有一个将此布局设置为其内容的 Activity ,为用户输入调用一个对话框,该对话框又调用 PuzzleView 中的一个函数以最终更改 PuzzleView 的内容。问题是,更改并没有在对话框关闭后立即重新绘制。相反,它会在用户的下一个其他输入时重新绘制。以下是一些可能对您有所帮助的代码 fragment :

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

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

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接口(interface)并在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();

关于android - invalidate() 不会立即更改自定义 View 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10264990/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com