0

在android中,我必须实现以下目标:

如果输入的密码是正确的funToEnterPassword();。我怎么能知道我从哪个方法调用了这个方法,所以我可以继续使用functionABC();functionXYZ();

public void fun1(){
 funToEnterPassword();
 funcABC();
}


public void fun1(){
 funToEnterPassword();
 functionXYZ();
}


public void funToEnterPassword(){
 //Enter password in popup
 //If password is correct how could I know here from which method I got called this method so I can continue with functionABC() or functionXYZ();
} 
4

3 回答 3

2

您可以使用 boolean 将方法类型声明为 boolean 或变量,并根据需要设置其值。简单的。:)

于 2012-12-27T06:48:07.600 回答
1

尝试以下方法:

public void fun1(){
   boolean result = funToEnterPassword();
   if (result) 
      funcABC();
}

public void fun2(){
   boolean result = funToEnterPassword();
   if (result)
      functionXYZ();
}


public boolean funToEnterPassword(){

   pwdResult = false;
   //Enter password in popup
   //If correct pwd
   pwdResult = true;
   //If password is correct how could I know here from which method I got called this             method so I can continue with functionABC() or functionXYZ();

   return pwdResult;
}
于 2012-12-27T06:57:16.277 回答
0

通常数组中的第三项应该保存当前的类和方法值!下面的代码可能会有一些用处!

StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
    if(stackTraceElements != null && stackTraceElements.length > 0){
        if(stackTraceElements.length > 2){
            String methodName = stackTraceElements[2].getMethodName();
            String className = stackTraceElements[2].getClassName();
            Log.e(className, methodName);
            Toast.makeText(this, className + " " + methodName, Toast.LENGTH_LONG).show();
        }

    }
于 2012-12-27T09:40:23.873 回答