我有一個包含自定義視圖中的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解散之後,該更改沒有被重新繪製。相反,它會重新繪製來自用戶的下一個其他輸入。 下面是一些代碼片斷,可以幫助你:
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;
}
只是用於測試的緣故,做'returnResult(4)'地方比別的clicklistener其他...硬編碼的瓷磚值,看看是否無效發生... – Ronnie 2012-04-23 13:58:45