按钮点击检测

时间:2013-04-02 11:32:48

标签: android

我正在研究这个计算出租车费用的android项目。它有一个按钮calculate和一个菜单选项night-mode。这个calculate方法确定两点之间的距离,以及随之而来的出租车费用。

night-mode根据我们在这里工作的夜间驾驶室的不同费率计算出租车费。 night-mode的基本要求是,只有当calculate进入onClick事件/且方法执行时,它才能运行。

在我的菜单选项中...如何检查按钮是否真的被点击/或onClick事件被触发?

1 个答案:

答案 0 :(得分:0)

创建一个状态变量,该变量在您的calculate-method之后在OnClick之后更改,并在单击nightmode时检查您的变量是否具有特定值(均为按钮版本)

示例:

    int integer=0;

    public void onClick(View v) {
            if (v == calculate){
                    if (integer == 0)
                            integer =1; //this makes your integer variable 1 if calculate is pressed the first time

                    else if (integer == 1)
                            integer=0;//this makes your integer variable 0 if calculate is pressed the second time(at the end of the drive)
    //do calculate things
    }
    else if (v == nightmode){
            if(integer == 1){
            //do nightmode things
            }
            else{}
    }