开发者

How to control AlertDialog

开发者 https://www.devze.com 2023-03-16 21:56 出处:网络
I have created an app which includes popups of different dialogs. Here is my code: if (lDiffFromToday >= 0 && lDiffFromToday <= DeclareVariable.CYCLE_MAX_LENGTH)

I have created an app which includes popups of different dialogs.

Here is my code:

if (lDiffFromToday >= 0 && lDiffFromToday <= DeclareVariable.CYCLE_MAX_LENGTH)
{
    AlertDialog.Builder alrtStartMonitoring = new AlertDialog.Builder(this);
    alrtStartMonitoring.setTitle("              Start Monitoring");
    alrtStartMonitoring.setMessage("Set start date of cycle as"+" "+sdFormatter.format(dtSelDate));
    alrtStartMonitoring.setPositiveButton("Yes", this);
    AlertDialog alert = alrtStartMonitoring.create();
    alert.show();
}   
else if (dtSelDate.getTime()> dtStartDate.getTime() && dtSelDate.getTime() <= currentDate.getTime() && !bCycleStopped)
{
    long lDiffFromStart =dtSelDate.getTime()-dtStartDate.getTime();
    lDiffFromStart=lDiffFromStart/(1000 * 60 * 60 * 24);
    if (lDiffFromStart >= DeclareVariable.CYCLE_MIN_LENGTH)
    {
        bActionOk = true;
        AlertDialog.Builder alrtStartMonitoring = new AlertDialog.Builder(this);
        alrtStartMonitoring.setTitle("              Confirm New Cycle");
        alrtStartMonitoring.setMessage("Set start date of cycle as" + " " + sdFormatter.format(dtSelDate));
        alrtStartMonitoring.setPositiveButton("Yes", this);
        AlertDialog alert = alrtStartMonitoring.create();
        alert.show();
    }
}

// ...

public void onClick(DialogInterface dialog, int id) 
{
    CycleManager.getSingletonObject().setHistoryDate(dtSelDate);
    int iStopStartCount = CycleManager.getSingletonObject().getStopStartCount();     开发者_StackOverflow社区           
    if(iStopStartCount>0)
        CycleManager.getSingletonObject().setStopStartDate(dtSelDate, iStopStartCount);
    displayDay();
}

Now my question is that for each dialog I need different onClick functions but in my case when I write another onClick function then there will be conflict. I know by writing the onClick function inside each dialog may solve the problem but in that case, I have to declare my variables as final so how can I do it by writing onClick function outside for every dialog I used.


Another solution will be to make the AlertDialog instances members of your class. Then in the OnClick method:

public void onClick(DialogInterface dialog, int id) 
{
  if (dialog == m_Dialog1)
  {
    // server dialog 1
  }

}


I can see that you've made the your class to implement the DialogInterface.OnClickListener

instead of

alrtStartMonitoring.setPositiveButton("Yes", this);

You can make it this way

alrtStartMonitoring.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

public void onClick (DialogInterface dialog, int which) {

}
});

and for each one of the setPositiveButton you can define the different onClick listener

Hope this helps


Why not create your own DialogInterface.OnClickListener classes and instantiate them with the appropriate variables from your main class that you don't want to mark as final, but do want them to have access to (inject them effectively). Then you can do

FooDialogOnClickListener l1 = new FooDialogOnClickListener(dtSelData, ...);
BarDialogOnClickListener l2 = new BarDialogOnClickListener(iStopStartCount, ...);

if (...) {
    // ...
    alrtStartMonitoring.setPositiveButton("Yes", l1);
} else {
    // ...
    alrtStartMonitoring.setPositiveButton("Yes", l2);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号