如何从对话框返回时失效()?

时间:2011-02-03 16:04:32

标签: android invalidation

我有一张带有OnTouchListener的绘图画布,我称之为AlertDialog

在对话框中,我重置了画布的基础数据(或不依赖于用户)。在使用dialog.cancel()返回画布时,画布不会重绘,我想要它 这意味着用户必须单击画布才能重绘 - 不好!

因为对话框异步运行,所以在对话框返回更改的基础数据之前,对画布中的invalidate()的任何调用都已完成。我在对话框按钮代码中无效或引用画布的任何尝试都会导致错误。我似乎陷入困境22!

enyone能给我建议吗?

对话框代码:

case DIALOG_NEW_ID:
    AlertDialog.Builder builder5 = new AlertDialog.Builder(this);
    builder5.setMessage("     WARNING" +
            "\nYour current game will be lost")
    .setCancelable(false)
    .setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {                   
            dialog.cancel();
        }
    })              
    .setNegativeButton("Continue", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            cells.reset();
            gameMode = 0;
            dialog.cancel();
        }
    });
    AlertDialog alert5 = builder5.create();             
    alert5.setOnDismissListener(new DialogInterface.OnDismissListener() { 
        public void onDismiss( DialogInterface dialog) { 
            Log.d(TAG, "Dialog cancelled");// debug log entry 
            //myBoard.invalidate();
        }
    });     
    dialog = alert5;                
    break;

画布中的对话框调用代码onTouch

    case MODE_SOLUTION:
        break;
    default:                            
        showDialog(DIALOG_NEW_ID);
        invalidate();
}// end switch  

主类设置:

public class SudokuGame extends Activity {
static final int DIALOG_OPEN_ID = 0;
static final int DIALOG_INFO_ID = 1;
static final int DIALOG_FAIL_ID = 2;
static final int DIALOG_SAVE_ID = 3;
static final int DIALOG_NEW_ID = 4;
static final int MODE_ENTRY = 0;
static final int MODE_PLAY = 1;
static final int MODE_SHOW_ERRORS = 3;
static final int MODE_SOLUTION = 4;
final String TAG = "Game";
int  gameMode;
SKU cells;
View myBoard;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_LEFT_ICON);
    View myBoard = new NewBoard(this);        
    setContentView(myBoard);
    setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
    showDialog(DIALOG_OPEN_ID);        
}// end onCreate

    protected Dialog onCreateDialog(int id) {

嵌套在主类中的Class NewBoard:

    class NewBoard extends View implements OnTouchListener{
    int cW;  // canvas width
    int cH;  // canvas height
    int cellSize,textSize;
    int boardX,boardY;  
    int runner, tX, tY,pX,pY,selectorX,selectorY;
    int gap ;
    int btn1X,btn1Y, selected;
    int infoButtonX, infoButtonY,quitButtonX,quitButtonY;
    boolean btn1Pressed, btn2Pressed,  btn3Pressed, btn4Pressed, btn5Pressed;
    String btn1,btn2,btn3,btn4,btn5;

    public NewBoard(Context context){ // NewBoard Constructor
        super(context);
        this.setOnTouchListener(this);  
        cells = new SKU();
        readStateStore();
    }// end NewBoard Constructor

    @Override 
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();  
        Rect textRect = new Rect();     

2 个答案:

答案 0 :(得分:2)

您是否可以使用对话框和invalidate()发布您的情况代码,并返回错误消息?

我不明白为什么你不能这样做(使mapView无效的对话框):

    AlertDialog.Builder builderAccountInfo = new AlertDialog.Builder(this);
        builderAccountInfo.setMessage("TEST")
        .setCancelable(false)
        .setTitle("TEST")
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            changeUserCanvas();
            mapView.invalidate();
            dialog.cancel();
       }
   });
        AlertDialog alertAccountInfo = builderAccountInfo.create();

        alertAccountInfo.show();

错误可能来自您用来使其无效的上下文,但是有关您正在制作的画布的更改以及对话框代码的更多信息是必需的。

////////////////////////////////修改

看起来查看myBoard的本地和全局变量之间存在问题。

public class SudokuGame extends Activity {
static final int DIALOG_OPEN_ID = 0;
static final int DIALOG_INFO_ID = 1;
static final int DIALOG_FAIL_ID = 2;
static final int DIALOG_SAVE_ID = 3;
static final int DIALOG_NEW_ID = 4;
static final int MODE_ENTRY = 0;
static final int MODE_PLAY = 1;
static final int MODE_SHOW_ERRORS = 3;
static final int MODE_SOLUTION = 4;
final String TAG = "Game";
int  gameMode;
SKU cells;
View myBoard;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_LEFT_ICON);
    View myBoard = new NewBoard(this);        
    setContentView(myBoard);
    setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
    showDialog(DIALOG_OPEN_ID);        
}// end onCreate

在onCreate中查看myBoard是在本地初始化的,但由于本地范围,尚未初始化全局View myBoard。你有两个myBoard变量,只有一个被初始化。要快速解决这个问题,我会改变:

View myBoard = new NewBoard(this);

myBoard = new NewBoard(this); 

答案 1 :(得分:0)

两种可能性:

  • 使对话框不可取消
  • 为对话框设置一个OnCancelListener,然后调用invalidate()。