如何无效()从对话框的回报?对话框

2023-09-04 06:25:38 作者:浅嫣婉语

我有一个绘图画布与 OnTouchListener 从我称之为 AlertDialog

在对话框重置基础数据的画布(或不依赖于用户)。在返回到画布 dialog.cancel()画布不重绘,我想它。 这意味着用户必须点击画布上,以使其重绘 - 不好!

由于该对话框异步运行在画布上的任何调用无效()完成对话框返回改变底层数据之前。任何尝试,我作出无效或出现错误对话框按钮code结果里面引用的画布。我似乎是在追赶22!

可以enyone给我建议?

对话框code:

 案例DIALOG_NEW_ID:
    AlertDialog.Builder builder5中=新AlertDialog.Builder(本);
    builder5.setMessage(警告+
            \ n您当前的游戏将会丢失)
    .setCancelable(假)
    .setPositiveButton(取消,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话框,INT ID){
            dialog.cancel();
        }
    })
    .setNegativeButton(继续,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话框,INT ID){
            cells.reset();
            游戏模式= 0;
            dialog.cancel();
        }
    });
    AlertDialog alert5 = builder5.create();
    alert5.setOnDismissListener(新DialogInterface.OnDismissListener(){
        公共无效onDismiss(DialogInterface对话){
            Log.d(TAG,对话框取消); //调试日志条目
            //myBoard.invalidate();
        }
    });
    对话框= alert5;
    打破;
 
如图怎么把对话框变小,下面的确定点不到

在画布上

对话框调用code onTouch

 案例MODE_SOLUTION:
        打破;
    默认:
        的ShowDialog(DIALOG_NEW_ID);
        无效();
} //结束开关
 

主类设置:

 公共类SudokuGame延伸活动{
静态最终诠释DIALOG_OPEN_ID = 0;
静态最终诠释DIALOG_INFO_ID = 1;
静态最终诠释DIALOG_FAIL_ID = 2;
静态最终诠释DIALOG_SAVE_ID = 3;
静态最终诠释DIALOG_NEW_ID = 4;
静态最终诠释MODE_ENTRY = 0;
静态最终诠释MODE_PLAY = 1;
静态最终诠释MODE_SHOW_ERRORS = 3;
静态最终诠释MODE_SOLUTION = 4;
最终的字符串变量=游戏;
INT游戏模式;
SKU细胞;
查看myBoard;


@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_LEFT_ICON);
    查看myBoard =新NewBoard(本);
    的setContentView(myBoard);
    setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.ic_launcher);
    的ShowDialog(DIALOG_OPEN_ID);
} //结束的onCreate

    受保护的对话框onCreateDialog(INT ID){
 

本类NewBoard嵌套在主类,其中:

 类NewBoard扩展视图实现OnTouchListener {
    诠释CW; //画布宽度
    INT CH; //帆布高度
    INT单元大小,TEXTSIZE;
    INT boardX,boardY;
    INT亚军,TX,TY,像素,PY,selectorX,selectorY;
    INT差距;
    INT btn1X,btn1Y,选择;
    INT infoButtonX,infoButtonY,quitButtonX,quitButtonY;
    布尔BTN1 pressed,BTN2 pressed,btn3 pressed,btn4 pressed,btn5 pressed;
    字符串BTN1,BTN2,btn3,btn4,btn5;

    公共NewBoard(上下文的背景下){// NewBoard构造
        超(上下文);
        this.setOnTouchListener(本);
        细胞=新的SKU();
        readStateStore();
    } //结束NewBoard构造

    @覆盖
    保护无效的OnDraw(帆布油画){
        super.onDraw(画布);
        涂料粉刷=新的油漆();
        矩形textRect =新的矩形();
 

解决方案

两种可能性:

请在对话框不可撤销 设置的OnCancelListener的对话框,然后将调用无效()。

I have a drawing canvas with an OnTouchListener from which I call an AlertDialog.

In the dialog I reset the underlying data for the canvas (or not depending on user). On returning to the canvas with dialog.cancel() the canvas does not redraw and I want it to. This means the user has to click on the canvas to cause it to redraw - not good!

Because the dialog runs asynchronously any call to invalidate() in the canvas is completed before the dialog returns the changed underlying data. Any attempt I make to invalidate or reference the canvas from inside the dialog buttons code results in errors. I seem to be in catch 22!

Can enyone give me advice?

dialog code:

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;

Dialog calling code onTouch in the canvas:

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

The Main Class set up:

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) {

The Class NewBoard which is nested in the main class:

    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();     

解决方案

Two possibilities:

make the dialog non-cancelable set an OnCancelListener for the dialog that will then call invalidate().